快速排序

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 static void main(String[] args) {
int[] arr1 = {6, 1, 2, 23, 75, 6, 4, 7, 9, 3, 4, 5, 10, 8, 11, -1, 0};
int start = 0;
int end = arr1.length - 1;
System.out.print("首次排序-");
quickSort(arr1, start, end);
System.out.print("【排序结果1:" + Arrays.toString(arr1));
System.out.println("\n快排次数:" + quickSortCount);


}

static void quickSort(int[] arr, int start, int end) {
int left = start;
int right = end;
int base = arr[left];
System.out.print("【基数:" + base);
System.out.print("排序队列" + Arrays.toString(Arrays.copyOfRange(arr, start, end)));
while (left < right) {

while (left < right) { //从右边找到小于等于基准的树
if (arr[right] <= base) { //当右边出现比基准arr[left]小的数
arr[left] = arr[right];
break;
}
right--;
quickSortCount++;
}
//从左边找到小于等于基准的数
while (left < right) {
if (arr[left] > base) {
arr[right] = arr[left];
break;
}
left++;
quickSortCount++;
}
}
arr[left] = base;
System.out.println("\t排序结果:" + Arrays.toString(Arrays.copyOfRange(arr, start, end)) + "】");
if (left + 1 < end) {
System.out.print(String.format("【基数[%d]右边排序", base));
quickSort(arr, left + 1, end);
}
if (start < left - 1) {
System.out.print(String.format("【基数[%d]左边排序", base));
quickSort(arr, start, left - 1);
}
}