算法_00004_快速排序

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
public class QuickSort {

private static void quickSort(final int array[], final int originLeft, final int originRight) {

if (originLeft < originRight) {

int baseValue = array[originLeft];
int tmpLeft = originLeft;
int tmpRight = originRight;

while (tmpLeft != tmpRight) {

while (tmpLeft < tmpRight && array[tmpRight] >= baseValue) tmpRight--;
array[tmpLeft] = array[tmpRight];

while (tmpLeft < tmpRight && array[tmpLeft] <= baseValue) tmpLeft++;
array[tmpRight] = array[tmpLeft];

}

array[tmpRight] = baseValue;
quickSort(array, originLeft, tmpLeft - 1);
quickSort(array, tmpRight + 1, originRight);

}

}

public static void main(String[] args) {

int array[] = {10, 5, 4, 1, 5, 2, 7, 3, 0, 4, 9, 6, 8};

System.out.println("排序之前:");
for (int element : array) System.out.print(element + " ");

quickSort(array, 0, array.length - 1);

System.out.println("\n排序之后:");
for (int element : array) System.out.print(element + " ");

}
}

参考