-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathQuickSort.java
More file actions
39 lines (35 loc) · 1.11 KB
/
QuickSort.java
File metadata and controls
39 lines (35 loc) · 1.11 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
import lib.Sort;
import lib.IntArrayConsumer;
/** Naive quicksort recursive implementation. */
public class QuickSort implements IntArrayConsumer {
public void accept(final int[] in) {
acceptRecursive(in, 0, in.length - 1);
}
private static void acceptRecursive(
final int[] in,
final int leftI,
final int rightI) {
if (leftI < rightI) {
int smallI = leftI;
int bigI = leftI;
final int pivot = in[rightI];
while (bigI < rightI) {
final int big = in[bigI];
if (big < pivot) {
int smallBuf = in[smallI];
in[smallI] = big;
in[bigI] = smallBuf;
smallI++;
}
bigI++;
}
in[rightI] = in[smallI];
in[smallI] = pivot;
acceptRecursive(in, leftI, smallI - 1);
acceptRecursive(in, smallI + 1, rightI);
}
}
public static void main(String[] args) throws Throwable {
Sort.test(args[0], new QuickSort());
}
}