-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
343 lines (322 loc) · 10.6 KB
/
Sorting.java
File metadata and controls
343 lines (322 loc) · 10.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
/**
* Your implementation of various sorting algorithms.
*
* @author Sunho Park
* @version 1.0
* @userid spark901
* @GTID 903795870
*
* Collaborators: LIST ALL COLLABORATORS YOU WORKED WITH HERE
*
* Resources: LIST ALL NON-COURSE RESOURCES YOU CONSULTED HERE
*/
public class Sorting {
/**
* private helper method for swapping elements
*
* @param arr array that has items to swap
* @param index1 first index
* @param index2 second index
* @param <T> data type to sort
*/
private static <T> void swap(T[] arr, int index1, int index2) {
T temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
/**
* Implement selection sort.
*
* It should be:
* in-place
* unstable
* not adaptive
*
* Have a worst case running time of:
* O(n^2)
*
* And a best case running time of:
* O(n^2)
*
* @param <T> data type to sort
* @param arr the array that must be sorted after the method runs
* @param comparator the Comparator used to compare the data in arr
* @throws java.lang.IllegalArgumentException if the array or comparator is
* null
*/
public static <T> void selectionSort(T[] arr, Comparator<T> comparator) {
if (arr == null || comparator == null) {
throw new IllegalArgumentException("Null array or null comparator "
+ "cannot be sorted or sort the array.");
}
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (comparator.compare(arr[j], arr[minIndex]) < 0) {
minIndex = j;
}
}
swap(arr, i, minIndex);
}
}
/**
* Implement insertion sort.
*
* It should be:
* in-place
* stable
* adaptive
*
* Have a worst case running time of:
* O(n^2)
*
* And a best case running time of:
* O(n)
*
* @param <T> data type to sort
* @param arr the array that must be sorted after the method runs
* @param comparator the Comparator used to compare the data in arr
* @throws java.lang.IllegalArgumentException if the array or comparator is
* null
*/
public static <T> void insertionSort(T[] arr, Comparator<T> comparator) {
if (arr == null || comparator == null) {
throw new IllegalArgumentException("Null array or null comparator "
+ "cannot be sorted or sort the array.");
}
for (int n = 1; n < arr.length; n++) {
int i = n;
while (i != 0 && comparator.compare(arr[i - 1], arr[i]) > 0) {
swap(arr, i - 1, i);
i--;
}
}
}
/**
* Implement bubble sort.
*
* It should be:
* in-place
* stable
* adaptive
*
* Have a worst case running time of:
* O(n^2)
*
* And a best case running time of:
* O(n)
*
* NOTE: See pdf for last swapped optimization for bubble sort. You
* MUST implement bubble sort with this optimization
*
* @param <T> data type to sort
* @param arr the array that must be sorted after the method runs
* @param comparator the Comparator used to compare the data in arr
* @throws java.lang.IllegalArgumentException if the array or comparator is
* null
*/
public static <T> void bubbleSort(T[] arr, Comparator<T> comparator) {
if (arr == null || comparator == null) {
throw new IllegalArgumentException("Null array or null comparator "
+ "cannot be sorted or sort the array.");
}
int n = arr.length;
int lastSwapIndex = n;
while (lastSwapIndex != 0) {
lastSwapIndex = 0;
for (int i = 1; i < n; i++) {
if (comparator.compare(arr[i - 1], arr[i]) > 0) {
swap(arr, i - 1, i);
lastSwapIndex = i;
}
}
n = lastSwapIndex;
}
}
/**
* Implement merge sort.
*
* It should be:
* out-of-place
* stable
* not adaptive
*
* Have a worst case running time of:
* O(n log n)
*
* And a best case running time of:
* O(n log n)
*
* You can create more arrays to run merge sort, but at the end, everything
* should be merged back into the original T[] which was passed in.
*
* When splitting the array, if there is an odd number of elements, put the
* extra data on the right side.
*
* Hint: If two data are equal when merging, think about which subarray
* you should pull from first
*
* @param <T> data type to sort
* @param arr the array to be sorted
* @param comparator the Comparator used to compare the data in arr
* @throws java.lang.IllegalArgumentException if the array or comparator is
* null
*/
public static <T> void mergeSort(T[] arr, Comparator<T> comparator) {
if (arr == null || comparator == null) {
throw new IllegalArgumentException("Null array or null comparator "
+ "cannot be sorted or sort the array.");
}
if (arr.length <= 1) {
return;
}
int len = arr.length;
int midIdx = len / 2;
//sub array
T[] leftArr = (T[]) (new Object[midIdx]);
for (int k = 0; k < midIdx; k++) {
leftArr[k] = arr[k];
}
T[] rightArr = (T[]) (new Object[len - midIdx]);
for (int k = midIdx; k < len; k++) {
rightArr[k - midIdx] = arr[k];
}
mergeSort(leftArr, comparator);
mergeSort(rightArr, comparator);
int i = 0; // left
int j = 0; // right
while (i < midIdx && j < len - midIdx) {
if (comparator.compare(leftArr[i], rightArr[j]) <= 0) {
arr[i + j] = leftArr[i];
i++;
} else {
arr[i + j] = rightArr[j];
j++;
}
}
//when right becomes empty
while (i < leftArr.length) {
arr[i + j] = leftArr[i];
i++;
}
//when left becomes empty
while (j < rightArr.length) {
arr[i + j] = rightArr[j];
j++;
}
}
/**
* Implement LSD (least significant digit) radix sort.
*
* Make sure you code the algorithm as you have been taught it in class.
* There are several versions of this algorithm and you may not get full
* credit if you do not implement the one we have taught you!
*
* Remember you CANNOT convert the ints to strings at any point in your
* code! Doing so may result in a 0 for the implementation.
*
* It should be:
* out-of-place
* stable
* not adaptive
*
* Have a worst case running time of:
* O(kn)
*
* And a best case running time of:
* O(kn)
*
* You are allowed to make an initial O(n) passthrough of the array to
* determine the number of iterations you need.
*
* At no point should you find yourself needing a way to exponentiate a
* number; any such method would be non-O(1). Think about how how you can
* get each power of BASE naturally and efficiently as the algorithm
* progresses through each digit.
*
* Refer to the PDF for more information on LSD Radix Sort.
*
* You may use ArrayList or LinkedList if you wish, but it may only be
* used inside radix sort and any radix sort helpers. Do NOT use these
* classes with other sorts. However, be sure the List implementation you
* choose allows for stability while being as efficient as possible.
*
* Do NOT use anything from the Math class except Math.abs().
*
* @param arr the array to be sorted
* @throws java.lang.IllegalArgumentException if the array is null
*/
public static void lsdRadixSort(int[] arr) {
if (arr == null) {
throw new IllegalArgumentException("Null array cannot be sorted.");
}
LinkedList<Integer>[] buckets = (LinkedList<Integer>[]) new LinkedList[19];
for (int i = 0; i < 19; i++) {
buckets[i] = new LinkedList<>();
}
int div = 1;
boolean loop = true;
while (loop) {
loop = false;
for (int num : arr) {
int bucket = num / div;
if (bucket / 10 != 0) {
loop = true;
}
buckets[bucket % 10 + 9].add(num);
}
int arrIdx = 0; // array index
for (LinkedList<Integer> bucket : buckets) {
while (!bucket.isEmpty()) {
Integer data = bucket.removeFirst();
arr[arrIdx++] = data;
}
}
div *= 10;
}
}
/**
* Implement heap sort.
*
* It should be:
* out-of-place
* unstable
* not adaptive
*
* Have a worst case running time of:
* O(n log n)
*
* And a best case running time of:
* O(n log n)
*
* Use java.util.PriorityQueue as the heap. Note that in this
* PriorityQueue implementation, elements are removed from smallest
* element to largest element.
*
* Initialize the PriorityQueue using its build heap constructor (look at
* the different constructors of java.util.PriorityQueue).
*
* Return an int array with a capacity equal to the size of the list. The
* returned array should have the elements in the list in sorted order.
*
* @param data the data to sort
* @return the array with length equal to the size of the input list that
* holds the elements from the list is sorted order
* @throws java.lang.IllegalArgumentException if the data is null
*/
public static int[] heapSort(List<Integer> data) {
if (data == null) {
throw new IllegalArgumentException("Null data cannot be in the array.");
}
PriorityQueue<Integer> minHeap = new PriorityQueue<>(data);
int[] result = new int[data.size()];
for (int i = 0; i < data.size(); i++) {
result[i] = minHeap.remove();
}
return result;
}
}