快速排序

#include<iostream.h>
//快速排序
//起核心算法是划分子序列
int Partition(int *a,int p,int r)
{
    int x=a[p];
    int i=p,j=r;
    while(true)
    {
        while(a[j]>x)
            j--;
        while(a[i]<x)
            i++;
        if(i<j)
        {
            if(a[i]==a[j]) //打破重复元素
                j--;
            else
            {
            int temp=a[i];a[i]=a[j];a[j]=temp;
            }
        }
        else
            return j;
    }
}
void Quick_sort(int *a,int p,int r)
{
    if(p<r)
    {
        int q=Partition(a,p,r);
        Quick_sort(a,p,q-1);
        Quick_sort(a,q+1,r);
    }
}
void main()
{
    int a[]={5,5};
    Quick_sort(a,0,1);
    for(int i=0;i<2;i++)
        cout<<a[i]<<" ";
}
原文地址:https://www.cnblogs.com/GoAhead/p/2726926.html