快速排序

来源:互联网 发布:mac好用的绘图 编辑:程序博客网 时间:2024/06/16 20:49

Problem Description

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

Input

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

Output

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

Example Input

849 38 65 97 76 13 27 49

Example Output

13 27 38 49 49 65 76 97

code:

#include <stdio.h>#include <stdlib.h>void dnf(int a[], int l, int r){    int x, k, j;    k=l, j=r, x=a[l];    if(l>=r)        return;    while(k<j)    {        while(k<j&&a[j]>=x)            j--;        a[k]=a[j];        while(k<j&&a[k]<=x)            k++;        a[j]=a[k];    }    a[k]=x;    dnf(a, l, k-1);    dnf(a, k+1, r);}int main(){    int n, i, a[100001];    while(scanf("%d", &n)!=EOF)    {        for(i=0; i<n; i++)        {            scanf("%d", &a[i]);        }        dnf(a, 0, n-1);        for(i=0; i<n; i++)        {            printf("%d%c", a[i], i==n-1?'\n':' ');        }    }    return 0;}