快速排序算法

template<class Type>
int Partition(Type a[],int p,int r)
{
 int i=p,j=r+1;
 Type x=a[p];
 while(true)
 {
  while(a[++i]<x&&i<r);
  while(a[--j]>x);
  if(i>=j)break;
         swap(a[i],a[j]);
 }
 a[p]=a[j];
 a[j]=x;
 return j;
}
template<class Type>
void QuickSort(Type a[],int p,int r)
{
 if(p<r)
 {
  int q=Partition(a,p,r);
        QuickSort(a,p,q-1);
  QuickSort(a,q+1,r);
 }
}

 

 

//調用形式

  QuickSort<int>(a,0,length);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章