-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMergeSort.java
53 lines (48 loc) · 1.39 KB
/
MergeSort.java
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
53
import java.util.Arrays;
public class MergeSort {
public static void merge(int array [], int start , int mid , int end) {
int combinedarray [] = new int [end-start+1];
int i= start;
int j = mid+1;
int k =0;
while(i<=mid && j<=end) {
if(array[i]<=array[j]){
combinedarray[k] = array[i];
k++;
i++;
} else {
combinedarray[k]= array[j];
k++;
j++;
}
}
while(i<=mid){
combinedarray[k]= array[i];
k++;
i++;
}
while(j<=end) {
combinedarray[k] = array[j];
k++;
j++;
}
for( int r= 0, p=start;r<combinedarray.length;r++ ,p++) {
System.out.println(combinedarray[r]+ "==" + p);
array[p]= combinedarray[r];
}
}
public static void mergeSort(int array[], int start , int end) {
if(start>=end) {
return ;
}
int mid = start +(end-start)/2;
mergeSort(array, start, mid);
mergeSort(array, mid+1, end);
merge(array ,start, mid , end);
}
public static void main(String[] args) {
int array [] = {4,5,11,2,1,34,54,12,18};
mergeSort(array, 0, array.length-1);
System.out.println(Arrays.toString(array));
}
}