View Code
#include<stdio.h>
#define N 100
#define MAX 10000000
void Merge(int a[], int low, int mid, int high)
{
int n1 = mid-low+1;
int n2 = high-mid;
int i;
int x[N],y[N];//将要合并的两部分存放到两个数组中
for(i = 1; i <= n1; i++)
{
x[i] = a[i+low-1];
}
for(i = 1; i <= n2; i++)
{
y[i] = a[low+n1+i-1];
}
int k = low, j = 1;
i = 1;
while(i <= n1 && j <= n2)
{
if(x[i] <= y[j])
a[k] = x[i++];
else
a[k] = y[j++];
k++;
}
while(i <= n1)
a[k++] = x[i++];
while(j <= n2)
a[k++] = y[j++];
}
void Merge_Sort(int a[], int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
Merge_Sort(a, low, mid);
Merge_Sort(a, mid+1, high);
Merge(a, low, mid, high);
}
}
int main()
{
int a[N],n;
int i;
scanf("%d",&n);//输入元素个数
for(i = 0; i < n; i++)
{
scanf("%d",a+i);
}
Merge_Sort(a,0,n-1);
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
putchar('\n');
return 0;
}