(归并排序)

(归并排序)输入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]);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值