Cifer

永远在内心的最深处听见水手说 他说风雨中这点痛算什么 擦干泪不要怕至少我们还有梦 他说风雨中这点痛算什么 擦干泪不要问为什么 好水 好水 好水 好水 好水 好水 好水 好水 !!!!...

HDOJ 1040 As Easy As A+B 快排
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
 

Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.
 

Output
For each case, print the sorting result, and one line one case.
 

Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
 

Sample Output
1 2 3
1 2 3 4 5 6 7 8 9
#include <stdio.h>

int num[1002];
int Partition(int left, int right)
{
    int x = num[left], t = 0;
    while(left<right)
    {
        while(num[right]>=x && left<right) right--;
        t = num[right];
        num[right] = num[left];
        num[left] = t;
        while(num[left]<=x && left<right) left++;
        t = num[right];
        num[right] = num[left];
        num[left] = t;
    }
    return right;
}
void q_sort(int left, int right)
{
    int min = 0;
    if(left >= right)
        return;
    else
    {
        min = Partition(left, right);
        q_sort(left, min-1);
        q_sort(min+1, right);
    }
}

int main()
{
    int i = 0, t = 0, n = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i = 0; i<n; i++)
        {
            scanf("%d",&num[i]);
        }
        q_sort(0, n-1);
        for(i = 0; i<n-1; i++)
            printf("%d ",num[i]);
        printf("%d",num[n-1]);
        printf("\n");
    }
    return 0;
}



阅读更多
个人分类: 其他 ACM做题笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

HDOJ 1040 As Easy As A+B 快排

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭