public class QuickSort
{
public static int partional(int[] a,int low,int high){
int pivotKey=(low+high)/2;
int temp;
if(a[low]>a[high]){
temp=a[low];
a[low]=a[high];
a[high]=temp;
}
if(a[pivotKey]>a[high]){
temp=a[pivotKey];
a[pivotKey]=a[high];
a[high]=temp;
}
if(a[low]<a[pivotKey]){
temp=a[low];
a[low]=a[pivotKey];
a[pivotKey]=temp;
}
temp=a[low];//以上都是为了找到一个合适的靶子作为划分数据,防止出现选择的是最大或者最小的数据
while(low<high)
{//
while(low<high&&a[high]>=temp){
high--;
}
a[low]=a[high];
while(low<high&&a[low]<=temp){
low++;
}
a[high]=a[low];
}//
a[high]=temp;//a[low]=temp
return high;//return low 跳出条件是low==high
}
public static void quickSort(int[] a,int low,int high){
int pivot;
if(low<high)
{
pivot=partional(a,low,high);
quickSort(a,low,pivot-1);
quickSort(a,pivot+1,high);
}
}
public static void main(String[] args)
{
int[] a = new int[]{9,1,5,8,3,7,4,6,2,12,22,14,16,34,100,17,23,56,140,0,11};
//{5,4,7,2,8,6,1,0,9,3};
quickSort (a,0,a.length-1);
for(int ele:a)
System.out.print(ele+" ");
}
}