Just Sort It(快速排序)

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 T(1\leq T \leq 10), the number of test cases. There is only line for each test case. The first number N(1\leq N \leq 250,000)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 [-2^{31},2^{31}-1]\,.


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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值