public class QuickSortTest {
public static void main(String[] args){
int a[]={20, 3, 5, 18, 90, 100, 30, 21, 8, 19};
quickSort(a, 0, a.length - 1); //指定从数组的哪个位置到哪个位置进行排序
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
public static void quickSort(int a[], int start, int end){
int temp = start;
int i = start;
int j = end;
while(i != j){
if(temp == i){
for(;j > i; j--){
if(a[temp] > a[j]){
swap(a, temp, j);
temp = j;
i++;
break;
}
}
}
if(temp == j){
for(;i < j; i++){
if(a[temp] < a[i]){
swap(a, temp, i);
temp = i;
j--;
break;
}
}
}
}
/*
* 左、右两边重新递归调用该排序算法,请注意if语句里面的条件判断,
* 是start<temp-1而不是start<temp,如果写后面的就会报栈溢出的错误
*/
if(start < temp - 1){
quickSort(a, start, temp - 1);
}
if(temp + 1 < end){
quickSort(a, temp + 1, end);
}
}
/*
* 将数组中的两个数进行交换
*/
private static void swap(int[] a, int t, int e) {
int temp = a[t];
a[t] = a[e];
a[e] = temp;
}
}