-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
51 lines (40 loc) · 1.27 KB
/
Copy pathMergeSort.java
File metadata and controls
51 lines (40 loc) · 1.27 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
package sorting;
public class MergeSort {
public static void mergeSort(int[] array) {
if (array.length <= 1) {
return;
}
int mid = array.length / 2;
int[] left = new int[mid];
int[] right = new int[array.length - mid];
System.arraycopy(array, 0, left, 0, left.length);
System.arraycopy(array, left.length, right, 0, right.length);
mergeSort(left);
mergeSort(right);
merge(array, left, right);
}
public static void merge(int[] result, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result[k++] = left[i++];
} else {
result[k++] = right[j++];
}
}
System.arraycopy(left, i, result, k, left.length - i);
System.arraycopy(right, j, result, k, right.length - j);
}
public static void main(String[] args) {
int[] array = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
for (int num : array) {
System.out.print(num + " ");
}
mergeSort(array);
System.out.println("\nSorted array:");
for (int num : array) {
System.out.print(num + " ");
}
}
}