HDOJ 1040 As Easy As A+B 快排

原创 2012年03月29日 20:41:48
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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

hdu1040 As Easy As A+B(C语言)

Problem Description These days, I am thinking about a question, how can I get a problem as easy a...

As Easy As A+B 1040

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU-1040- As Easy As A+B-选择排序+冒泡排序

其实贴这道题目是为了给新手介绍一下这几个简单排序方法; 首先我们看一下选择排序,思路很简单,就是从第一个开始每次定位一个数,和数组中最大(最小)的数交换一下位置; 不过这是个不稳定排序算法; #...
  • wlxsq
  • wlxsq
  • 2015-07-14 16:40
  • 423

HDU 1040 As Easy As A+B

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

[1040]:As Easy As A+B(将输入的数排序)

As Easy As A+BTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T...

杭电ACM 1040 As Easy As A+B java 解读

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

HDU1040As Easy As A+B

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

hdu 1040 As Easy As A+B 各种排序

各种排序。

As Easy As A+B

As Easy As A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

as easy as a+b

  • 2011-06-02 00:35
  • 135B
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)