数据结构实验之排序八:快速排序 递归

来源:互联网 发布:个人备案域名做淘宝客 编辑:程序博客网 时间:2024/06/05 16:42

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

给定N(N≤10^5)个整数,要求用快速排序对数据进行升序排列,注意不得使用STL。

Input

连续输入多组数据,每组输入数据第一行给出正整数N(≤10^5),随后给出N个整数,数字间以空格分隔。

Output

输出排序后的结果,数字间以一个空格间隔,行末不得有多余空格。

Example Input

8

49 38 65 97 76 13 27 49

Example Output

13 27 38 49 49 65 76 97

#include <stdio.h>int a[3100000];void qsort(int l,int r){    int i=l,j=r;    if(l>=r) return;    int k=a[l];    while(i<j)    {        while(i<j&&a[j]>=k) j--;        a[i]=a[j];        while(i<j&&a[i]<=k) i++;        a[j]=a[i];    }    a[i]=k;    qsort(l,i-1);    qsort(i+1,r);}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        qsort(0,n-1);        for(int i=0; i<n; i++)        {            if(i!=n-1) printf("%d ",a[i]);            else printf("%d\n",a[i]);        }    }    return 0;}
0 0
原创粉丝点击