Luo Gu [template] Quick Sort

Copyright: blogger original, welcome to reprint. Please indicate the source https://blog.csdn.net/baidu_41248654/article/details/78698148

Topic links: https://www.luogu.org/problemnew/show/1177

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num[100010],n;
void quicksort(int a[],int L,int R)
{
    int i=L,j=R;
    int mid=a[(L+R)/2];
    while(i<=j)
    {
        while(a[i]<mid)
            i++;
        while(a[j]>mid)
            j--;
        if(i<=j)
        {
            swap(a[i],a[j]);
            i++;
            j--;
        }
    }
    if(j>L)
        quicksort(a,L,j);
    if(i<R)
        quicksort(a,i,R);
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&num[i]);
    quicksort(num,1,n);
    for(int i=1; i<=n; i++)
        printf("%d ",num[i]);
    return 0;
}

Although you can sort through it again, but in order to grasp the essence of fast sorting algorithm . . . Ahem, it is recommended to write about, after all, it is not very difficult.

Guess you like

Origin blog.csdn.net/baidu_41248654/article/details/78698148