快速排序算法的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另外一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
class PartitionSort{
public void p(int[] a){ //打印输出结果
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
public int Partition(int[] a,int low,int high){ //找出中间位置的序号,并返回
int temp = a[low]; //set the low number is middle number
while(low<high){
while(low<high&&temp<=a[high]){ //this is can not lost =,else is die loop
high--; //high position - 1
}
a[low]=a[high]; //< middle number ,make it in front of middle number
while(low<high&&temp>a[low]){
low++;
}
a[high]=a[low];
}
a[low] = temp;
return low;
}
public void QSort(int[] a,int low,int high){ //recurrence method to come true 使用递归方法进行排序
if(a.length>0){
if(low<high){
int partition = Partition(a,low,high);
QSort(a,low,partition-1);
QSort(a,partition+1,high);
}
}
}
public static void main(String args[]){
int[] a = {38,49,65,97,76,13,27,49};
PartitionSort P = new PartitionSort();
P.QSort(a,0,a.length-1);
P.p(a);
}
}