-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathMergeSort.java
More file actions
70 lines (63 loc) · 1.61 KB
/
MergeSort.java
File metadata and controls
70 lines (63 loc) · 1.61 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
package misc;
/**
* Steps:-
* 1) Divide the unsorted array into n partitions, each partition
* contains 1 element. Here the one element is considered as sorted.
* 2) Repeatedly merge partitioned units to produce new sublists until there is
* only 1 sublist remaining. This will be the sorted list at the end.
*
*/
public class MergeSort {
private int[] array;
private int[] tmp;
private int length;
public static void main(String[] args) {
int[] input = { 45, 23, 11, 89, 77, 98, 4, 28, 65, 43 };
MergeSort ms = new MergeSort();
ms.sort(input);
for (int i : input) {
System.out.print(i);
System.out.print(" ");
}
}
public void sort(int[] input) {
this.array = input;
this.length = input.length;
this.tmp = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// sorts the left side of the array
doMergeSort(lowerIndex, middle);
// sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tmp[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tmp[i] <= tmp[j]) {
array[k] = tmp[i];
i++;
} else {
array[k] = tmp[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tmp[i];
k++;
i++;
}
}
}