【数据结构】快速排序

◇◆丶佛笑我妖孽 提交于 2019-12-17 09:19:16
#include<iostream>

using namespace std;
int a[100000];
int i,j;
int par(int r[],int first,int end)
{
    i=first,j=end;
    r[0]=r[i];
    while(i<j)
    {
        while(i<j&&r[0]<=r[j]) j--;
        if(i<j){
            r[i]=r[j];i++;
        }
        while(i<j&&r[i]<=r[0]) i++;
        if(i<j) {r[j]=r[i];j--;}
    }
    r[i]=r[0];
    return i;
}
int pos;
void quickSort(int r[],int first,int end)
{
    if(first<end){
    pos=par(r,first,end);
    quickSort(r,first,pos-1);
    quickSort(r,pos+1,end);}
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    quickSort(a,1,n);
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!