1004 归并排序
#include <iostream>
using namespace std;
int temp[1000];
int res[1000];
void merge(int a[], int low, int middle, int high){
int i=low,k=low,j=middle+1;
while(i <= middle && j <= high){
if(a[i]<=a[j])
temp[k++]=a[i++];
else temp[k++]=a[j++];
}
while(i<=middle)
temp[k++]=a[i++];
while(j<=high)
temp[k++]=a[j++];
for(k=low;k<=high;k++)
a[k] = temp[k];
return;
}
void merge_sort(int a[], int low, int high, int level)
{
if (low >= high)
{
return;
}
int middle = (low+high)/2;
merge_sort(a, low, middle, level+1);
merge_sort(a, middle+1, high, level+1);
merge(a, low, middle, high);
if(level==3)
{
for(int i=low;i<=high;i++)
{
res[i] = a[i];
}
}
}
void print(int a[], int size)
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int size;
cin>>size;
int a[size];
for(int i=0;i<size;i++)
{
cin>>a[i];
}
merge_sort(a, 0, size-1, 1);
print(res, size);
}
return 0;
}