准备校招,居然快速排序手写不出来。受不了!!!!
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
int [] array ={1,2,5,6,3,4,7,8,9,12,11,0};
quickSort(array,0,array.length-1);
System.out.println("quickSor结果="+Arrays.toString(array));
}
private static void quickSort(int[] array, int low, int high) {
// TODO Auto-generated method stub
if(low>high){return;} // 快排中用到递归,这个不能丢啊
int [] a=array;
int i = low;
int j = high;
int index = a[i];
while(i<j){ // 时刻预防越界
while(i<j&&a[j]>index){
j--;
}
if(i<j){
a[i++] = a[j];
}
while(i<j && a[i]<index){
i++;
}
if(i<j){
a[j--]=a[i];
}
}
a[i] = index;
quickSort(a,low,i-1);
quickSort(a,i+1,high);
}
}