-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
53 lines (46 loc) · 1.21 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
package sorting;
public class MergeSort
{
public static void sort(int[] in) {
if(in.length < 2) return; //do not need to sort
int mid = in.length/2;
int left[] = new int[mid];
int right[] = new int[in.length-mid];
for(int i=0; i<mid; i++) { //copy left
left[i] = in[i];
}
for(int i=0; i<in.length-mid; i++){ //copy right
right[i] = in[mid+i];
}
sort(left);
sort(right);
merge(left, right, in);
}
private static void merge(int[] a, int[] b, int[] all) {
int i=0, j=0, k=0;
while(i<a.length && j<b.length) { //merge back
if(a[i] < b[j]){
all[k] = a[i];
i++;
}else{
all[k] = b[j];
j++;
}
k++;
}
while(i<a.length) { //left remaining
all[k++] = a[i++];
}
while(j<b.length) { //right remaining
all[k++] = b[j++];
}
}
public static void main(String[] args) {
int[] a = {2,3,6,4,9,22,12,1};
sort(a);
System.out.println("Sorted array is:");
for(int j=0; j<a.length; j++) {
System.out.print(a[j] + " ");
}
}
}