/**
* 快速排序(基于霍尔的快排)
*/
public int Partition(int a[],int low,int high){
int key = a[low];
int temp;
int i = low;int j = high;
while(i<j){
while(i<j && a[i]<=key) i++;
while(i<=j && a[j]>=key) j--; //注意这里的=号不能少
if(i<j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
//把j对应的位置和key进行交换
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
public void QuickSort(int a[],int low,int high){
if(low<high){
int mid = Partition(a,low,high);
QuickSort(a,low,mid-1);
QuickSort(a,mid+1,high);
}
}
public static void main(String [] args){
Test t = new Test();
int [] array = new int[]{20,5,3,1,9,8,2,4,7,10};
t.QuickSort(array,0,array.length-1);
System.out.println(Arrays.toString(array));
}
/**
* 快排
* @param array
* @param low
* @param hight
* @return
*/
public static int partition(int []array,int low,int hight){
int key=array[low];
while(low<hight){
while(array[hight]>=key&&hight>low){
hight--;
}
array[low]=array[hight];
while(array[low]<=key&&hight>low){
low++;
}
array[hight]=array[low];
}
array[hight]=key;
return low;
}
public static void sort(int[] array,int low ,int hight){
if(low>=hight){
return ;
}
int index=partition(array,low,hight);
sort(array,low,index-1);
sort(array,index+1,hight);
}