public class QuickSort {
public void quickSort(int[] a, int left, int right){
int i, j, t, temp;
if (left > right)
return;
temp = a[left]; //哨兵
i = left;
j = right;
while (i != j){
while (a[j] >= temp && i < j) //从右往左找
j--;
while (a[i] <= temp && i < j) //从左往右找
i++;
if (i < j){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
quickSort(a, left, i-1);
quickSort(a, i+1, right);
}
public static void main(String[] args){
//int[] arr = {8, 3, 1, 0, 5, 6, 2, 7, 1};
int[] arr = {25, 15,27,99,18,35,14,66};
new QuickSort().quickSort(arr, 0, arr.length-1);
for(int i : arr){
System.out.println(i);
}
}
}
10-04
10-04