-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
49 lines (47 loc) · 1.11 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
import java.util.Arrays;
public class MergeSort
{
public static int counter;
public static void main(String args[])
{
int [] a = {8,7,6,5,4,3,2,1,7,9};
a=sort(a);
System.out.println(Arrays.toString(a) + " counter: " + counter + " length: " + a.length);
}
public static int[] sort(int[] a){
if (a.length==1) {
return a;
} else {
return merge(sort(Arrays.copyOfRange(a, 0, a.length/2)),sort(Arrays.copyOfRange(a, a.length/2, a.length)));
}
}
public static int[] merge (int[] a, int[] b){
//counter++;
int[] temp = new int[a.length+b.length];
int positiona = 0;
int positionb = 0;
int positiontemp = 0;
while (positiona<a.length && positionb<b.length){
if(a[positiona] <= b[positionb]) {
temp[positiontemp] = a[positiona];
positiona++;
positiontemp++;
} else {
temp[positiontemp] = b[positionb];
positionb++;
positiontemp++;
}
}
while(positiona<a.length) {
temp[positiontemp] = a[positiona];
positiona++;
positiontemp++;
}
while(positionb<b.length) {
temp[positiontemp] = b[positionb];
positionb++;
positiontemp++;
}
return temp;
}
}