-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathQuickSortTest.java
More file actions
77 lines (73 loc) Β· 1.98 KB
/
QuickSortTest.java
File metadata and controls
77 lines (73 loc) Β· 1.98 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
class QuickSortTest
{
public static void asc(int[] array)
{
array = ascQuickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static void desc(int[] array)
{
array = descQuickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static int[] ascQuickSort(int[] array, int left, int right)
{
int tmp;
if (left < right) {
int i = left;
int j = right;
int pivot = array[i + (j - i) / 2];
while (true) {
while (array[i] < pivot) {
i++;
}
while (pivot < array[j]) {
j--;
}
if (i >= j) {
break;
}
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
ascQuickSort(array, left, i - 1);
ascQuickSort(array, j + 1, right);
}
return array;
}
public static int[] descQuickSort(int[] array, int left, int right)
{
int tmp;
if (left < right) {
int i = left;
int j = right;
int pivot = array[i + (j - i) / 2];
while (true) {
while (array[i] > pivot) {
i++;
}
while (pivot > array[j]) {
j--;
}
if (i >= j) {
break;
}
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
descQuickSort(array, left, i - 1);
descQuickSort(array, j + 1, right);
}
return array;
}
}