-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
163 lines (154 loc) · 5.15 KB
/
MergeSort.java
File metadata and controls
163 lines (154 loc) · 5.15 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
public class MergeSort {
static void sort(int[] array, boolean isDesc) {
int arrayLength = array.length;
if (arrayLength < 2) {
return;
}
// breaking the array into two halves
int midPoint = arrayLength / 2;
int[] left = new int[midPoint];
int[] right = new int[arrayLength - midPoint];
for (int i = 0; i < midPoint; i++) {
left[i] = array[i];
}
for (int i = midPoint; i < arrayLength; i++) {
right[i - midPoint] = array[i];
}
// recursivly breaking the two halves we just creating until we reach a
// length of 1, then merge sort them after
sort(left, isDesc);
sort(right, isDesc);
merge(array, left, right, isDesc);
}
private static void merge(int[] mergedArray, int[] left, int[] right, boolean isDesc) {
int leftLength = left.length;
int rightLength = right.length;
int i = 0, j = 0, k = 0;
while (i < leftLength && j < rightLength) {
if ((!isDesc && (left[i] <= right[j])) || (isDesc && (left[i] >= right[j]))) {
mergedArray[k] = left[i];
i++;
} else {
mergedArray[k] = right[j];
j++;
}
k++;
}
// we might still have remaining sorted elements in either halves of the array
while (j < rightLength) {
mergedArray[k] = right[j];
j++;
k++;
}
while (i < leftLength) {
mergedArray[k] = left[i];
i++;
k++;
}
}
static void sort(char[] array, boolean isDesc) {
int arrayLength = array.length;
if (arrayLength < 2) {
return;
}
int midPoint = arrayLength / 2;
char[] left = new char[midPoint];
char[] right = new char[arrayLength - midPoint];
for (int i = 0; i < midPoint; i++) {
left[i] = array[i];
}
for (int i = midPoint; i < arrayLength; i++) {
right[i - midPoint] = array[i];
}
// recursivly breaking the two halves we just creating until we reach a
// length of 1, then merge sort them after
sort(left, isDesc);
sort(right, isDesc);
merge(array, left, right, isDesc);
}
private static void merge(char[] mergedArray, char[] left, char[] right, boolean isDesc) {
int leftLength = left.length;
int rightLength = right.length;
int i = 0, j = 0, k = 0;
while (i < leftLength && j < rightLength) {
if ((!isDesc && Character.compare(left[i], right[j]) <= 0)
|| (isDesc && (Character.compare(right[j], left[i]) <= 0))) {
mergedArray[k] = left[i];
i++;
} else {
mergedArray[k] = right[j];
j++;
}
k++;
}
// we might still have remaining sorted elements in either halves of the array
while (j < rightLength) {
mergedArray[k] = right[j];
j++;
k++;
}
while (i < leftLength) {
mergedArray[k] = left[i];
i++;
k++;
}
}
static void sort(String[] array, boolean isDesc) {
int arrayLength = array.length;
if (arrayLength < 2) {
return;
}
int midPoint = arrayLength / 2;
String[] left = new String[midPoint];
String[] right = new String[arrayLength - midPoint];
for (int i = 0; i < midPoint; i++) {
left[i] = array[i];
}
for (int i = midPoint; i < arrayLength; i++) {
right[i - midPoint] = array[i];
}
// recursivly breaking the two halves we just creating until we reach a
// length of 1, then merge sort them after
sort(left, isDesc);
sort(right, isDesc);
merge(array, left, right, isDesc);
}
private static void merge(String[] mergedArray, String[] left, String[] right, boolean isDesc) {
int leftLength = left.length;
int rightLength = right.length;
int i = 0, j = 0, k = 0;
while (i < leftLength && j < rightLength) {
if ((!isDesc && (left[i].compareTo(right[j]) <= 0))
|| (isDesc && (right[j].compareTo(left[i]) <= 0))) {
mergedArray[k] = left[i];
i++;
} else {
mergedArray[k] = right[j];
j++;
}
k++;
}
// we might still have remaining sorted elements in either halves of the array
while (j < rightLength) {
mergedArray[k] = right[j];
j++;
k++;
}
while (i < leftLength) {
mergedArray[k] = left[i];
i++;
k++;
}
}
// to give the user ability to call the sorting method without the isDesc
// argument
static void sort(int[] array) {
sort(array, false);
}
static void sort(char[] array) {
sort(array, false);
}
static void sort(String[] array) {
sort(array, false);
}
}