(归并排序)输入10个数,升序后输出。
*21 80 71 66 48 71 40 90 29 22 *
21 22 29 40 48 66 71 71 80 90
1 23 14 52 11 4 5 7 13 22
1 4 5 7 11 13 14 22 23 52*
#include<stdio.h>
#define max 10
void merging(int *fun1,int fun1_long,int *fun2,int fun2_long)
{
int t[max];
int i,j,h;
i = 0;
j = 0;
h = 0;
while (i < fun1_long && j < fun2_long)
{
if (fun1[i] < fun2[j])
t[h++] = fun1[i++];
else
t[h++] = fun2[j++];
}
while (i < fun1_long)
t[h++] = fun1[i++];
while (j < fun2_long)
t[h++] = fun2[i++];
for (int m = 0;m < (fun1_long+fun2_long);m++)
fun1[m] = t[m];
}
void MergeSort(int a[],int n )
{
if (n > 1)
{
int *fun1 = a;
int fun1_long = n/2;
int *fun2 = a+n/2;
int fun2_long = n-fun1_long;
MergeSort(fun1,fun1_long);
MergeSort(fun2,fun2_long);
merging(fun1,fun1_long,fun2,fun2_long);
}
}
main()
{
int a[10];
for (int i = 0;i < 10;i++)
{
scanf (“%d”,&a[i]);
}
MergeSort(a,10);
for (int i = 0;i < 10;i++)
{
printf (“%d”,a[i]);
}
}