快速排序模板

Posted by 清水咲太 on September 25, 2022
#include<bits/stdc++.h>
#define N 5000005

using namespace std;

int n, a[N];

void qsort(int j, int k){
int o = j, p = k, flag = (o + p) / 2;
do{
while(a[o] < a[flag]){
o++;
}
while(a[p] > a[flag]){
p--;
}
if(o <= p){
swap(a[p], a[o]);
o++; p--;
}
}while(o <= p);
if(j < o)qsort(j, p);
if(j < p)qsort(o, k);
return ;
}

int main () {
cin>>n;
for(int i = 1; i <= n; i++)cin>>a[i];
qsort(1, n);
for(int i = 1; i <= n; i++)cout<<a[i]<<" ";
return 0;
}