package sort;
public class QuickSort {
/*
* 思想:通过一趟排序将待排的记录分成两部分
* 其中一部分比关键字均比另一部分的关键字小
* 分别对这两部分记录继续递归排序
*/
public static int quickSort(int a[],int low,int high){
int key=a[low]; // key=10
while(low<high){ // 两边向中间移动,直到low=high
while(low<high&&a[high]>=key) // 5?10
high--;
a[low]=a[high]; // 不满足a[high]>=key执行
while(low<high&&a[low]<=key)// 5?5
low++;// +1
a[high]=a[low];
}
a[low]=key;// 中间值了 low=high了,关键位置,循环结束
System.out.println(low+"------------"+high);
return low;
}
public static void Qsort(int a[],int low,int high){
int pos;
if(low<high){
pos=quickSort(a,low,high); // 运用递归再对依次排序进行排序
Qsort(a,low,pos-1);
Qsort(a,pos+1,high);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={10,9,8,7,6,5,4,3,2,1};
System.out.println("位置:"+quickSort(a,0,a.length-1));
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"-->");
}
//--------------------------------------------------------
System.out.println("-------------------------------/n");
int aa[]={10,9,8,7,6,5,4,3,2,1,0,0,1};
Qsort(aa,0,aa.length-1);
for(int j=0;j<aa.length;j++){
System.out.print(aa[j]+"--->");
}
}
}