-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
72 lines (61 loc) · 1.84 KB
/
Copy pathMergeSort.java
File metadata and controls
72 lines (61 loc) · 1.84 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
// Divide the unsorted array into n partitions, each partition contains 1 element.
// Repeatedly merge partitioned units to produce new sublists until there is only
// 1 sublist remaining. This will be the sorted list at the end.
// Merge sort is a fast and stable sorting routine with guaranteed O(lgn * n) efficiency.
// When sorting arrays, merge sort requires additional scrath space proportional to the
// size of the input array. Merge sort is relatively simple to code and offeres performance
// typically only slightly below that of quicksort.
class MergeSort {
private int[] array;
private int[] tmpArray;
private int length;
public static void main(String[] args) {
int[] array = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
MergeSort m = new MergeSort();
m.sort(array);
printArray(array);
}
public void sort( int[] array ) {
this.array = array;
this.length = array.length;
this.tmpArray = new int[length];
mergeSort(0, length - 1);
}
private void mergeSort (int start, int end) {
if (start < end) {
int mid = (start + end) / 2;
mergeSort(start, mid);
mergeSort(mid+1, end);
mergeParts(start, mid, end);
}
}
private void mergeParts(int start, int mid, int end) {
for (int i = start; i <= end; i++) {
tmpArray[i] = array[i];
}
int i = start;
int j = mid + 1;
int k = start;
while (i <= mid && j <= end) {
if (tmpArray[i] <= tmpArray[j]) {
array[k] = tmpArray[i];
i++;
} else {
array[k] = tmpArray[j];
j++;
}
k++;
}
while (i <= mid) {
array[k] = tmpArray[i];
k++;
i++;
}
}
private static void printArray(int[] input) {
for (int i = 0; i < input.length - 1; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(input[input.length-1]);
}
}