-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
51 lines (44 loc) · 950 Bytes
/
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
45
46
47
48
49
50
51
public class QuickSort {
/**
* @param args
*/
public static void main(String[] args) {
int[] a = { 87655, 9, 2, 1, 7, 3, 888 };
int start = 0;
int end = a.length - 1;
qSort(a, start, end);
print(a);
}
private static void qSort(int[] a, int start, int end) {
if (start < end) {
int pIndex = partition(a, start, end);
qSort(a, start, pIndex - 1);
qSort(a, pIndex + 1, end);
}
}
private static int partition(int[] a, int start, int end) {
int pIndex = start;
int pivot = a[end];
for (int i = start; i < end; i++) {
if (a[i] < pivot) {
swap(i, pIndex, a);
pIndex++;
}
}
swap(end, pIndex, a);
return pIndex;
}
private static void swap(int i, int j, int[] B) {
int temp = 0;
temp = B[i];
B[i] = B[j];
B[j] = temp;
}
private static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
System.out.print(",");
}
System.out.println();
}
}