Just Sort It(快速排序)

来源:互联网 发布:淘宝店粉丝怎么看 编辑:程序博客网 时间:2024/06/05 09:04

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 numberN(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

33 3 2 12 3 23 5 4 3


Sample Output

1 2 32 33 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
原创粉丝点击