-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortA.java
More file actions
56 lines (53 loc) · 1.36 KB
/
MergeSortA.java
File metadata and controls
56 lines (53 loc) · 1.36 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
52
53
54
55
56
import java.util.Arrays;
/**
* MergeSortA
*/
public class MergeSortA {
public static void mergeSort(int[] A) {
int n = A.length;
if (n >= 2){
//return;
int[] L = new int[n / 2];
int[] R = new int[n - n / 2];
for (int i = 0; i < n / 2; i++) {
L[i] = A[i];
}
for (int i = n / 2; i < n; i++) {
R[i - n / 2] = A[i];
}
//System.out.println(Arrays.toString(L) + "\n");
mergeSort(L);
mergeSort(R);
merge(L, R, A);}
}
public static void merge(int[] L, int[] R, int[] a) {
int nL = L.length, nR = R.length, i = 0, j = 0, k = 0;
while (i < nL && j < nR) {
if (L[i] <= R[j]) {
a[k] = L[i];
i++;
} else {
a[k] = R[j];
j++;
}
k++;
}
while (i < nL) {
a[k] = L[i];
i++;
k++;
}
while (j < nR) {
a[k] = R[j];
j++;
k++;
}
//System.out.println(Arrays.toString(a) + "\n");
}
public static void main(String[] args) {
int[] A = { 8, 5, 2, 6, 9, 3, 1, 4, 0, 7 };
System.out.println(Arrays.toString(A) + "\n");
mergeSort(A);
System.out.println(Arrays.toString(A) + "\n");
}
}