题目
给定你一个长度为 n 的整数数列。
请你使用快速排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n 个整数(所有整数均在 1∼109 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
模板
#include<iostream>
using namespace std;
const int N=1e6+10;
int p[N];
int n;
void quick_sort(int p[],int l,int r)
{
if(l>=r) return;
int x=(p[l]+p[r])/2,i=l-1,j=r+1;
while(i<j)
{
do i++;while(x>p[i]);
do j--;while(x<p[j]);
if(i<j) swap(p[i],p[j]);
}
quick_sort(p,l,j);
quick_sort(p,j+1,r);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&p[i]);
quick_sort(p,0,n-1);
for(int i=0;i<n;i++)
printf("%d ",p[i]);
return 0;
}