A - The experimental data structure of a sort: trip fast row

Description

Integer in the given range of the N long integer, required output results of a given number of data for a first trip after the quick sort pivoted.

Input

Enter multiple sets of data, each data input of the first row is given a positive integer N (N <= 10 ^ 5 ), an integer in the range of the N long integer given subsequently, separated by a space between the numbers.
Output

The result of a trip output quick sort, with a space between the digital gap, the end of the line may not have the extra space.
Sample

Input

8
49 38 65 97 76 13 27 49
Output

27 38 13 49 76 97 65 49

#include <stdio.h>
#include <string.h>
#include<algorithm>
using namespace std;
int a[100010];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        int i = 0,j = n-1;
        int x = a[0];
        while(i<j)
        {
            while(i<j&&x<=a[j])//注意一定要有等号
                j--;
            a[i] = a[j];
            while(i<j&&x>=a[i])
                i++;
            a[j] = a[i];
        }
        a[i] = x;
        for(int i=0;i<n-1;i++)
            printf("%d ",a[i]);
        printf("%d\n",a[n-1]);
    }
    return 0;
}


Published 176 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104930921