Posted on 2012-05-08 17:11
hoshelly 閱讀(132)
評論(0) 編輯 收藏 引用 所屬分類:
Programming
輸入第一行包含一個整數T,表示有T組數據。對于每組數據:第一行包含一個數字N(<100),表示該組數據由N個元素;第二行包含N個數,就是這N個元素的值( <10000 )。
輸出對于每組數據輸出一行,包含排序好后的N個元素,要求從小到大排序,相鄰2個元素間有個空格,末尾無空格有個回車。
樣例輸入1 3 1 3 2
樣例輸出1 2 3
#include<stdio.h>
void swap(int a[],int low,int high)
{
int temp=a[low];
a[low]=a[high];
a[high]=temp;
}
int partition(int a[],int low,int high)
{
int pivotkey=a[low];
while(low<high)
{
while(low<high && a[high]>=pivotkey)
high--;
swap(a,low,high);
while(low<high && a[low]<=pivotkey)
low++;
swap(a,low,high);
}
return low;
}
void qsort(int a[],int low,int high)
{
int pivot;
if(low<high)
{
pivot=partition(a,low,high);
qsort(a,low,pivot-1);
qsort(a,pivot+1,high);
}
}
int main()
{
int n,m,i,a[102];
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
qsort(a,0,m-1);
for(i=0;i<m;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
return 0;
}