数据结构快速排序是最流行的排序算法,因为有充足的理由,在大多数情况下,快速排序算法是最快的。那下面我们来了解一下快速排序的具体算法程序。
在我看来快速排序是基于划分算法的迭代。下面就是基本代码结构:
public void recQuitSort(int left,int riht){
if(left>=right)
return;
else{
long pivot=array[right];//每次确定数据最右端的数据位枢纽
int partition=patitionIt(left,right,pivot);
recQuitSort(left,partition-1);
recQuitSort(partition+1,right);
}
}
public int patitionIt(int low,int high,long pivot){
int leftPtr=low-1;
int rightPtr=high;
while(true){
while(theArray[++leftPtr]<pivot);//为什么这没有条件leftPrt<right,因为leftPrt最终会停在枢纽位置上的
while(rightPtr>0&&theArray[--rightPtr]>pivot);
if(leftPtr>=rightPtr)
break;
else
swap(leftPtr,rightPtr);
}
swap(leftPtr,high);//将枢纽调换到数组中间 因为leftPrt总会停在数据比枢纽数据大的位置上
return leftPtr;//返回中间位置
}
public void swap(int one,int two){
long temp=theArray[one];
theArray[one]=theArray[two];
theArray[two]=temp;
}
下面附一次循环过程图: