分治——排序(1 <= n <= 100000)

问题 B: 排序(1 <= n <= 100000)

时间限制: 10 Sec  内存限制:128 MB

题目描述

输入n组测试数据,从小到大排序。

输入

输出

样例输入

2
3
3 6 5
4
8 5 9 7

样例输出

3 6 5
5 7 8 9

提示

(1  <= n <= 100000)

#include<iostream>
using namespace std;
int temp;
int a[100000];
void qsort(int le,int ri)
{
    int i=le,j=ri,mid=a[(le+ri)/2];
    while(i<=j)
    {
      while(a[i]<mid)i++;
      while(a[j]>mid)j--;
      if(i<=j)
      {
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
          i++;
          j--;
      }
    }
    if(j>le)qsort(le,j);
    if(i<ri)qsort(i,ri);
}
int main()
{
    int n,t,le,ri;
    cin>>n;
    while(n--)
    {
        cin>>t;
        for(int i=0;i<t;i++)
            cin>>a[i];
        qsort(0,t-1);
        cout<<a[0];
        for(int i=1;i<t;i++)
            cout<<" "<<a[i];
        cout<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40729773/article/details/79720068
今日推荐