-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMergeSort.java
More file actions
115 lines (104 loc) · 2.98 KB
/
MergeSort.java
File metadata and controls
115 lines (104 loc) · 2.98 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.examplehub.sorts;
import com.examplehub.maths.MiddleIndexCalculate;
public class MergeSort implements Sort {
@Override
public void sort(int[] numbers) {
mergeSort(numbers, 0, numbers.length - 1);
}
@Override
public <T extends Comparable<T>> void sort(T[] array) {
mergeSort(array, 0, array.length - 1);
}
/**
* Merge sort algorithm implements.
*
* @param numbers the numbers to be sorted.
* @param left the left index of sub array.
* @param right the right index of sub array.
*/
public static void mergeSort(int[] numbers, int left, int right) {
if (left < right) {
int middle = MiddleIndexCalculate.middle(left, right);
mergeSort(numbers, left, middle);
mergeSort(numbers, middle + 1, right);
merge(numbers, left, right);
}
}
/**
* Generic MergeSort algorithm implements.
*
* @param array the array to be sorted.
* @param left the left index of sub array.
* @param right the right index of sub array.
* @param <T> the class of the objects in the array.
*/
public static <T extends Comparable<T>> void mergeSort(T[] array, int left, int right) {
if (left < right) {
int middle = (left + right) >> 1;
mergeSort(array, left, middle);
mergeSort(array, middle + 1, right);
merge(array, left, right);
}
}
/**
* @param numbers the numbers to be merged.
* @param left the left index of first sub array.
* @param right the right index of second sub array.
*/
public static void merge(int[] numbers, int left, int right) {
int middle = (left + right) >> 1;
int i = left;
int j = middle + 1;
int k = 0;
int len = right - left + 1;
int[] temp = new int[len];
while (i <= middle && j <= right) {
if (numbers[i] <= numbers[j]) {
temp[k++] = numbers[i++];
} else {
temp[k++] = numbers[j++];
}
}
while (i <= middle) {
temp[k++] = numbers[i++];
}
while (j <= right) {
temp[k++] = numbers[j++];
}
for (int number : temp) {
numbers[left++] = number;
}
}
/**
* Generic merge two sub sorted numbers.
*
* @param array the array to be sorted.
* @param left the left index of first sub array.
* @param right the right index of second sub array.
* @param <T> the class of the objects in the array.
*/
public static <T extends Comparable<T>> void merge(T[] array, int left, int right) {
int middle = (left + right) >> 1;
int i = left;
int j = middle + 1;
int k = 0;
@SuppressWarnings("unchecked")
T[] tempArray = (T[]) new Comparable[right - left + 1];
while (i <= middle && j <= right) {
if (array[i].compareTo(array[j]) <= 0) {
tempArray[k++] = array[i++];
} else {
tempArray[k++] = array[j++];
}
}
while (i <= middle) {
tempArray[k++] = array[i++];
}
while (j <= right) {
tempArray[k++] = array[j++];
}
for (T temp : tempArray) {
array[left++] = temp;
}
}
}