Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
88 lines (77 loc) · 2.17 KB

File metadata and controls

88 lines (77 loc) · 2.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Sorts;
/**
* Generic merge sort algorithm.
*
* @see SortAlgorithm
*/
class MergeSort implements SortAlgorithm {
/**
* Generic merge sort algorithm implements.
*
* @param unsorted the array which should be sorted.
* @param <T> Comparable class.
* @return sorted array.
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
doSort(unsorted, 0, unsorted.length - 1);
return unsorted;
}
/**
* @param arr the array to be sorted.
* @param left the first index of the array.
* @param right the last index of the array.
*/
private static <T extends Comparable<T>> void doSort(T[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) >>> 1;
doSort(arr, left, mid);
doSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
/**
* Merges two parts of an array.
*
* @param arr the array to be merged.
* @param left the first index of the array.
* @param mid the middle index of the array.
* @param right the last index of the array merges two parts of an array in increasing order.
*/
private static <T extends Comparable<T>> void merge(T[] arr, int left, int mid, int right) {
int length = right - left + 1;
@SuppressWarnings("unchecked")
T[] temp = (T[]) new Comparable[length];
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right) {
if (arr[i].compareTo(arr[j]) <= 0) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
System.arraycopy(temp, 0, arr, left, length);
}
/** Driver code */
public static void main(String[] args) {
MergeSort mergeSort = new MergeSort();
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
mergeSort.sort(arr);
for (int i = 0; i < arr.length - 1; ++i) {
assert arr[i] <= arr[i + 1];
}
String[] stringArray = {"c", "a", "e", "b", "d"};
mergeSort.sort(stringArray);
for (int i = 0; i < stringArray.length - 1; ++i) {
assert arr[i].compareTo(arr[i + 1]) <= 0;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.