重生之——我是菜鸡,重刷算法基础题1
作者:
Bonker
,
2024-01-05 11:01:32
,
所有人可见
,
阅读 171
#include<iostream>
using namespace std;
const int N = 100010;
int n;
int a[N];
void quick_sort(int st,int ed){
if(st >= ed) return;
int i = st - 1;
int j = ed + 1;
int k = a[(st + ed) / 2];
while(i < j){
do{i++;}while(a[i] < k);
do{j--;}while(a[j] > k);
if(i < j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
quick_sort(st,j);
quick_sort(j+1,ed);
}
void solve(){
cin >> n;
for(int i = 0;i < n;i++) cin >> a[i];
quick_sort(0,n-1);
for(int i = 0;i < n;i++) cout << a[i] << " ";
}
int main(){
solve();
}