-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting.java
115 lines (105 loc) · 3.35 KB
/
Sorting.java
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
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
/**
* Your implementation of various iterative sorting algorithms.
*/
public class Sorting {
/**
* 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: You should implement bubble sort with the last swap optimization.
*
* You may assume that the passed in array and comparator
* are both valid and will never be null.
*
* @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.
*/
public static <T> void bubbleSort(T[] arr, Comparator<T> comparator) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
int stopIndex = arr.length - 1;
while(stopIndex > 0) {
int lastSwap = 0;
for (int i = 0; i < stopIndex; i++) {
if(comparator.compare(arr[i], arr[i + 1])> 0) {
swap(arr, i, i +1);
lastSwap = i;
}
}
stopIndex = lastSwap;
}
}
/**
* 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)
*
* You may assume that the passed in array and comparator
* are both valid and will never be null.
*
* @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.
*/
public static <T> void selectionSort(T[] arr, Comparator<T> comparator) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
for(int swapIndex = arr.length -1; swapIndex > 0; swapIndex--) {
int max= 0;
for (int i = 1; i <= swapIndex;i++) {
if(comparator.compare(arr[i], arr[max]) > 0) {
max = i;
}
}
swap(arr, max, swapIndex);
}
}
/**
* 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)
*
* You may assume that the passed in array and comparator
* are both valid and will never be null.
*
* @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.
*/
public static <T> void insertionSort(T[] arr, Comparator<T> comparator) {
// WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
for(int unsortedStart = 1; unsortedStart < arr.length; unsortedStart++){
int i = unsortedStart;
while(i > 0 && comparator.compare(arr[i - 1], arr[i]) > 0){
swap(arr, i -1, i);
i--;
}
}
}
private static <T> void swap(T[] arr, int a, int b) {
T temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}