数据结构实验之排序八:快速排序
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给定N(N≤10^5)个整数,要求用快速排序对数据进行升序排列,注意不得使用STL。
Input
连续输入多组数据,每组输入数据第一行给出正整数N(≤10^5),随后给出N个整数,数字间以空格分隔。
Output
输出排序后的结果,数字间以一个空格间隔,行末不得有多余空格。
Sample Input
8 49 38 65 97 76 13 27 49
Sample Output
13 27 38 49 49 65 76 97
Hint
Source
#include <iostream>
#include<stdio.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn];
int n;
void pre(int a[],int l,int r)
{
if(l>=r) return;
int key=a[l];
int i=l;
int j=r;
while(i<j)
{
while(i<j&&a[j]>=key)j--;
a[i]=a[j];
while(i<j&&a[i]<=key)i++;
a[j]=a[i];
}
a[i]=key;
pre(a,l,i-1);
pre(a,i+1,r);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
pre(a,0,n-1);
for(int i=0; i<n; i++)
{
if(i==0)
printf("%d",a[i]);
else printf(" %d",a[i]);
}
cout<<endl;
}
return 0;
}