-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergesort.java
More file actions
51 lines (41 loc) · 1.33 KB
/
Mergesort.java
File metadata and controls
51 lines (41 loc) · 1.33 KB
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
import java.util.*;
//http://programmingcode4life.blogspot.my/2015/09/merge-sort.html#more
public class Mergesort{
public static void main(String[] args){
int [] a = {22,63,22,54,102,1,38,36,89,98,99,200,36};
mergeSort(a);
System.out.println(Arrays.toString(a));
}
public static void mergeSort(int []a){
int[] tmp = new int[a.length];
mergeSort(a,tmp,0,a.length - 1);
}
private static void mergeSort(int []a, int []tmp, int left, int right){
if( left < right ){
int center = (left + right) / 2;
mergeSort(a, tmp, left, center);
mergeSort(a, tmp, center + 1, right);
merge(a, tmp, left, center + 1, right);
}
}
private static void merge(int[]a,int[]tmp,int left,int right,int rightEnd ){
int leftEnd = right - 1;
int k = left;
int num = rightEnd - left + 1;
while(left <= leftEnd && right <= rightEnd){
if(a[left] < a[right])
tmp[k++] = a[left++];
else
tmp[k++] = a[right++];
}
// Copy rest of first half
while(left <= leftEnd)
tmp[k++] = a[left++];
// Copy rest of right half
while(right <= rightEnd)
tmp[k++] = a[right++];
// Copy temp array back
for(int i = 0; i < num; i++, rightEnd--)
a[rightEnd] = tmp[rightEnd];
}
}