-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuickSort.java
More file actions
199 lines (148 loc) · 4.6 KB
/
Copy pathQuickSort.java
File metadata and controls
199 lines (148 loc) · 4.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
package Sort;
import Extra.Stopwatch;
import java.util.Random;
public class QuickSort {
private static final Random random = new Random();
private static final int INSERTION_THRESHOLD = 47;
private QuickSort() {
}
/**
* Sorts the entire array of integers.
*/
public static void sort(int[] a) {
sort(a, 0, a.length - 1);
}
/**
* Sorts the array of integers from the low index to
* the high one (both included).
*
* @param a
* @param low
* @param high
*/
public static void sort(int[] a, int low, int high) {
if (high - low < INSERTION_THRESHOLD) {
Insertion.sort(a, low, high);
return;
}
int pivotIndex = getRandomIndex(low, high);
int separationIndex = partition(a, low, high, pivotIndex);
sort(a, low, separationIndex - 1);
sort(a, separationIndex + 1, high);
}
/**
* Partitions the array from the low index to high
* index (both included) around the pivot at the
* given pivot index.
*
* @param a
* @param low
* @param high
* @param pivotIndex
*/
public static int partition(int[] a, int low, int high, int pivotIndex) {
int pivot = a[pivotIndex];
swap(a, pivotIndex, high);
int storeIndex = low;
for (int i = low; i < high; i++) {
if (pivot > a[i]) {
swap(a, i, storeIndex++);
}
}
swap(a, storeIndex, high);
return storeIndex;
}
/**
* Sorts the entire array of comparable objects.
*/
public static void sort(Comparable[] a) {
sort(a, 0, a.length - 1);
}
/**
* Sorts the array of comparable objects from the low index to
* the high one (both included).
*
* @param a
* @param low
* @param high
*/
public static void sort(Comparable[] a, int low, int high) {
if (high - low < INSERTION_THRESHOLD) {
Insertion.sort(a, low, high);
return;
}
int pivotIndex = getRandomIndex(low, high);
int separationIndex = partition(a, low, high, pivotIndex);
sort(a, low, separationIndex - 1);
sort(a, separationIndex + 1, high);
}
/**
* Partitions the array from the low index to high
* index (both included) around the pivot at the
* given pivot index.
*
* @param a
* @param low
* @param high
* @param pivotIndex
*/
public static int partition(Comparable[] a, int low, int high, int pivotIndex) {
Comparable pivot = a[pivotIndex];
swap(a, pivotIndex, high);
int storeIndex = low;
for (int i = low; i < high; i++) {
if (pivot.compareTo(a[i]) > 0) {
swap(a, i, storeIndex++);
}
}
swap(a, storeIndex, high);
return storeIndex;
}
private static int getRandomIndex(int low, int high) {
return random.nextInt(high - low + 1) + low;
}
private static void swap(int[] a, int index1, int index2) {
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
private static void swap(Comparable[] a, int index1, int index2) {
Comparable temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
public static void printArray(int[] a) {
System.out.println();
for (int i : a) {
System.out.print(i + " ");
}
}
public static void main(String[] args) {
int[] abc = new int[]{7, 6, 3, 4, 1, 6, 5, 2, 8};
MergeSort.sort(abc);
assertArrayIsSorted(abc);
QuickSort.printArray(abc);
System.out.println();
abc = new int[]{2, 3, 4, 7, 2, -1, 3, 55, 32, -7, 4, 3};
MergeSort.sort(abc);
assertArrayIsSorted(abc);
QuickSort.printArray(abc);
int size = (int) Math.pow(10., 8.);
System.out.println();
Random random = new Random();
Stopwatch stopwatch = new Stopwatch();
int[] randomArray = new int[size];
for (int i = 0; i < size; i++) {
randomArray[i] = random.nextInt();
}
System.out.println("Generated numbers in " + stopwatch.ellapsedTime());
stopwatch = new Stopwatch();
QuickSort.sort(randomArray);
System.out.println("It took " + stopwatch.ellapsedTime() + " seconds.");
}
public static void assertArrayIsSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) throw new Error("Doesn't work!");
}
}
}