-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
59 lines (47 loc) · 1.34 KB
/
Copy pathMergeSort.java
File metadata and controls
59 lines (47 loc) · 1.34 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
import java.util.Arrays;
public class MergeSort {
int[] a = {22, 58, 47, 96, 45, 32, 12};
int[] b = new int[a.length];
private void merge(int low, int mid, int high) {
int l1, l2, i;
l1 = low;
l2 = mid + 1;
i = low;
while (l1 <= mid && l2 <= high) {
if (a[l1] <= a[l2]) {
b[i] = a[l1];
l1++;
} else {
b[i] = a[l2];
l2++;
}
i++;
}
while (l1 <= mid) {
b[i++] = a[l1++];
}
while (l2 <= high) {
b[i++] = a[l2++];
}
for (i = low; i <= high; i++) {
a[i] = b[i];
}
}
private void sort(int low, int high) {
int mid;
if (low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid + 1, high);
merge(low, mid, high);
}
}
public static void main(String[] args) {
MergeSort obj = new MergeSort();
System.out.println("Array before sorting: ");
System.out.println(Arrays.toString(obj.a));
obj.sort(0, obj.a.length - 1);
System.out.println("Array after sorting: ");
System.out.println(Arrays.toString(obj.a));
}
}