HDOJ 1040 As Easy As A+B 快排

647人阅读 评论(0)
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;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：92181次
• 积分：2083
• 等级：
• 排名：第18294名
• 原创：120篇
• 转载：3篇
• 译文：0篇
• 评论：14条
评论排行
最新评论