-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuickSortTest.java
More file actions
74 lines (60 loc) Β· 1.4 KB
/
Copy pathQuickSortTest.java
File metadata and controls
74 lines (60 loc) Β· 1.4 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
package basic.b;
public class QuickSortTest {
static final int SIZE = 10;
static Pair[] array = new Pair[SIZE];
public static void main(String[] args) {
array[0] = new Pair(3, 4);
array[1] = new Pair(2, 1);
array[2] = new Pair(1, 4);
array[3] = new Pair(1, 2);
array[4] = new Pair(3, 2);
array[5] = new Pair(1, 0);
array[6] = new Pair(5, 5);
array[7] = new Pair(7, 2);
array[8] = new Pair(0, 3);
array[9] = new Pair(1, 33);
quickSort(0, SIZE - 1);
for (int i = 0; i < SIZE; i++) {
System.out.println(array[i]);
}
}
private static void quickSort(int left, int right) {
if (left >= right) {
return;
}
int i = left - 1;
int j = right + 1;
int pivot_x = array[(left + right) / 2].x;
int pivot_y = array[(left + right) / 2].y;
while (i < j) {
while ((array[++i].x < pivot_x) || (array[i].x == pivot_x) && (array[i].y < pivot_y)) {
}
while ((array[--j].x > pivot_x) || (array[j].x == pivot_x) && (array[j].y > pivot_y)) {
}
if (i >= j) {
break;
}
swap(i, j);
}
quickSort(left, i - 1);
quickSort(i + 1, right);
return;
}
private static void swap(int o1, int o2) {
Pair tmp = array[o1];
array[o1] = array[o2];
array[o2] = tmp;
return;
}
private static class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "[x=" + x + ", y=" + y + "]";
}
}
}