package sort;
import java.util.Arrays;
public class QuikSort2 {
public static void swap(int[]a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
/*
* 散数中指分割法
*/
private static int median3(int []a,int left,int right){
int center=(left+right)/2;
if(a[center]<a[left])
swap(a,left,center);
if(a[right]<a[left])
swap(a,left,right);
if(a[right]<a[center])
swap(a,center,right);
return a[right-1];
}
/*
* 对数组进行快速排序
*/
public static void quikSort(int a[],int left,int right){
int pivot=median3(a,left,right);//取枢纽
int i=left,j=right;
for(;;)//将数组划分为两部分
{
while(a[i]<pivot&&(i<j)){
i++;
}
while(a[j]>pivot&&(j>i)){
j--;
}
if(i<j)
swap(a,i,j);
else
break;
}
if (left < j)
quikSort(a,left,i-1);//前半部分排序
if (right > i)
quikSort(a,i+1,right);//后半部分排序
}
public static void main(String[] args) {
int []a={12,11,45,6,8,43,40,57,3,20};
System.out.println(Arrays.toString(a));
quikSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
}