Judge Info
- Memory Limit: 1124KB
- Case Time Limit: 10000MS
- Time Limit: 10000MS
- Judger: Number Only Judger
Description
Sorting is one of the most important algorithm in computer sciences. Please learn it well.
本题需要的排序算法复杂度应该为nlogn,冒泡、选择的负责度为n^2,快排、归并、堆排序的复杂度为nlogn
本题的内存限制也是比较严格,所以建议大家使用堆排序,或者快速排序
Input
The first line of input contains
, the number of test cases. There is only line for each test case. The first number
of each test case will indicates how many integer numbers are there for sort from smaller to larger. All numbers are in the
range of
.
Output
Output one line for every test case. The result of those integer numbers after sorting.
Sample Input
3 3 3 2 1 2 3 2 3 5 4 3
Sample Output
1 2 3 2 3 3 4 5
标准快排
#include<stdio.h>
void quicksort(int *s, int l, int r)
{
if (l < r)
{
int i = l, j = r, x = s[l];
while (i < j)
{
while(i < j && s[j] >= x) j--;
if(i < j) s[i++] = s[j];
while(i < j && s[i] < x) i++;
if(i < j) s[j--] = s[i];
}
s[i] = x;
quicksort(s, l, i - 1);
quicksort(s, i + 1, r);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int a[250000];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
for(int i=0;i<n;i++)
{
printf("%d",a[i]);
if(i<n-1)
putchar(' ');
}
putchar('\n');
}
return 0;
}
本文介绍了一种需要实现nlogn复杂度的排序算法挑战,并推荐使用快速排序或堆排序来应对严格的内存限制。提供了标准快速排序算法的C语言实现示例。
454

被折叠的 条评论
为什么被折叠?



