c++快速排序算法的实现

来源:互联网 发布:去重的sql语句 编辑:程序博客网 时间:2024/05/16 11:52

 

           //快速排序算法的实现,希望对初学者有所帮助

           #include <stdio.h>
           #include <iostream>

using namespace ::std;
int qpartition(int number[],int low,int high)
{
      int pivotvalue;
      pivotvalue = number[low];
      while(low < high)
      {
            while((low < high) && number[high] >= pivotvalue)--high;
            number[low] = number[high];
            while((low < high) && number[++low] <= pivotvalue);
            number[high--] = number[low];

     }
     number[low] = pivotvalue;
     return low;

}
void Qsort(int number[],int s,int t)
{
     int position;
     if (s < t)
     {
         position = qpartition(number,s,t);
         Qsort(number,s,position - 1);
         Qsort(number,position + 1,t);
     }

}
int main()
{
     int *number;
     int i=0,size;
    cout<<"请输入待排序个数:"<<endl;
    cin>>size;
    number = (int *)malloc(size * sizeof(int));
    while(i < size)
    {
        cin>>number[i++];
    }
    Qsort(number,0,size - 1);
    for (i=0;i<size;i++)
   {
       cout<<number[i];
   }
   free(number);
   return 0;
}