快速排序

代码

using namespace std; 
int shuzu[]={15,14,5,24,18,19,4,58,12};
void quicksort(int shuzu[],int left,int right);
int main()
{
    int left=0,right=8;
    quicksort(shuzu,left,right);
    for(left=0;left<9;left++)
        cout<<shuzu[left]<<" ";
    return 0;
}
void quicksort(int shuzu[],int left,int right)
{
    int f;
    int ltemp=left,rtemp=right;
    f=shuzu[(left+right)/2];
    while(ltemp<rtemp)
    {
        while(shuzu[ltemp]<f)
            ltemp++;
        while(shuzu[rtemp]>f)
            rtemp--;
        if(ltemp<rtemp)
        {
            int temp=shuzu[ltemp];
            shuzu[ltemp]=shuzu[rtemp];
            shuzu[rtemp]=temp;
        }
    }
        if(left<rtemp)
        {
            quicksort(shuzu,left,ltemp-1);
        }
        if(ltemp<right)
            quicksort(shuzu,rtemp+1,right);
        return;
}

 

posted @ 2018-03-08 20:00  kkdf  阅读(102)  评论(0编辑  收藏  举报