第一讲 基础算法
作者:互联网
1.快速排序
https://www.acwing.com/problem/content/787/
#include <iostream>
using namespace std;
const int N = 100010;
int n;
int a[N];
void quick_sort(int a[],int l,int r){
if(l >= r) return;
swap(a[l],a[(l+r) >> 1]);
int pivot = a[l];
int i = l,j = r;
while(i < j){
if(a[j] >= pivot && i < j) j--;
a[i] = a[j];
if(a[i] <= pivot && i < j) i++;
a[j] = a[i];
}
a[i] = pivot;
quick_sort(a,l,i-1);
quick_sort(a,i+1,r);
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
quick_sort(a,1,n);
for(int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
标签:sort,www,第一,int,namespace,基础,算法,quick,pivot 来源: https://www.cnblogs.com/superkcl/p/15859823.html