-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quicksort.java
44 lines (38 loc) · 1.22 KB
/
Quicksort.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
public class Quicksort {
public static void main(String[] args) {
int[] values = {5, 1, 3, 2, 4};
quicksort(values, 0, values.length);
for (int i : values) {
System.out.print(i + " ");
}
System.out.println();
}
public static void quicksort(int[] data, int left, int right) {
if (right - left < 2) {
return;
}
int pivot = data[left];
int leftPartitionLength = 0;
int rightPartitionLength = 0;
int index = left;
while (index < right - rightPartitionLength) {
if (data[index] > pivot) {
rightPartitionLength++;
swap(data, index, right - rightPartitionLength);
} else if (data[index] < pivot) {
swap(data, index, left + leftPartitionLength);
leftPartitionLength++;
index++;
} else {
index++;
}
}
quicksort(data, left, left + leftPartitionLength);
quicksort(data, right - rightPartitionLength, right);
}
private static void swap(int[] data, int a, int b) {
int tmp = data[a];
data[a] = data[b];
data[b] = tmp;
}
}