-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.java
More file actions
52 lines (45 loc) · 986 Bytes
/
Copy pathmergeSort.java
File metadata and controls
52 lines (45 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class mergeSort {
private static void sort(int nums[],int left,int right){
int mid = (left+right)/2;
if(left<right){
sort(nums,left,mid);
sort(nums,mid+1,right);
merge(nums,left,mid,right);
}
}
private static void merge(int[] nums,int left,int mid,int right){
int[] temp = new int[right-left+1];
int i=left;
int j=mid+1;
int k=0;
while(i<=mid && j<=right){
if(nums[i]<nums[j]){
temp[k++]=nums[i++];
}else{
temp[k++]=nums[j++];
}
}
while(i<=mid){
temp[k++]=nums[i++];
}
while(j<=right){
temp[k++]=nums[j++];
}
for(int p=0;p<temp.length;p++){
nums[left+p] = temp[p];
}
}
public static void main(String[] args) {
int[] nums = new int[100];
for(int i=0;i<nums.length;i++){
int num = (int) (Math.random()*1000);
nums[i]=num;
System.out.print(num+" ");
}
System.out.println();
sort(nums,0,nums.length-1);
for(int i=0;i<nums.length;i++){
System.out.print(nums[i]+" ");
}
}
}