-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_912_sort_an_array.java
More file actions
74 lines (65 loc) · 2 KB
/
Copy path_912_sort_an_array.java
File metadata and controls
74 lines (65 loc) · 2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Arrays;
public class _912_sort_an_array {
public static int[] merge(int[] a1, int[] a2) {
int n = a1.length + a2.length;
int n1 = a1.length;
int n2 = a2.length;
int i=0, i1=0, i2=0;
int[] result = new int[n];
while(i<n) {
if (i1 < n1 && i2 < n2) {
if(a1[i1] <= a2[i2]) {
result[i] = a1[i1];
i++;
i1++;
} else {
result[i] = a2[i2];
i++;
i2++;
}
} else {
if (i1 < n1) {
result[i] = a1[i1];
i++;
i1++;
} else {
result[i] = a2[i2];
i++;
i2++;
}
}
}
return result;
}
public static int[] mergeSort(int[] a, int L, int R) {
if (L > R) {
return new int[0]; // return null array
}
if (L == R) {
int[] singleElement = {a[L]};
return singleElement;
}
// split
// System.out.println("Split: " + L + " - " + R);
int k = (L+R)/2;
int[] a1 = mergeSort(a, L, k);
int[] a2 = mergeSort(a, k+1, R);
// merge: a1 and a2 is sorted
int[] result = merge(a1, a2);
// System.out.println("Merge result: " + Arrays.toString(result));
return result;
}
public static int[] sortArray(int[] nums) {
return mergeSort(nums, 0, nums.length-1);
}
public static void main(String[] args) {
// test merge(): WORK
// int[] a1 = {1,3,5,7,8,10};
// int[] a2 = {2,4,6,7,9};
// System.out.println(Arrays.toString(merge(a1, a2)));
int[] a = {1,5,3,2,8,7,6,4};
System.out.println(Arrays.toString(sortArray(a)));
// int[] result = sortArray(a);
System.out.println("DONE");
}
}