C语言实现堆排序和归并排序

堆排序

#include<stdio.h>
#define SWAP(X,Y) {X=X+Y;Y=X-Y;X=X-Y;}
/*整数数组输出函数*/
int myputs(int *array,int num)
{
int i;
for(i=0;i<num;i++)
    {
    printf("%3d ",array[i]);
    if(!((i+1)%20))printf("\n");
    }
printf("\n");
return 0;
}
/*堆排序(从小到大)*/
/*建堆(小顶堆)*/
int AdjustHeap_1(int *array,int num,int beg)
{
int i,end=num-1;
if(!((num-beg)%2))
{
    if(array[end]<array[(end-beg-1)/2+beg])
    {
        SWAP(array[end],array[(end-beg-1)/2+beg])
    }
    --end;
}
for(i=end;i>beg+1;i-=2)
{
    if(array[i-1]<array[(i-beg)/2-1+beg])
    {
        SWAP(array[i-1],array[(i-beg)/2-1+beg])
    }
    if(array[i]<array[(i-beg)/2-1+beg])
    {
        SWAP(array[i],array[(i-beg)/2-1+beg])
    }
}
return 0;
}
/*排序*/
int HeapSort_1(int *array,int num)
{
int i;
for(i=0;i<num-1;i++)
{
   AdjustHeap_1(array,num,i);
}
return 0;
}

/*堆排序(从大到小)*/
/*建堆(大顶堆)*/
int AdjustHeap_2(int *array,int num,int beg)
{
int i,end=num-1;
if(!((num-beg)%2))
{
    if(array[end]>array[(end-beg-1)/2+beg])
    {
        SWAP(array[end],array[(end-beg-1)/2+beg])
    }
    --end;
}
for(i=end;i>beg+1;i-=2)
{
    if(array[i-1]>array[(i-beg)/2-1+beg])
    {
        SWAP(array[i-1],array[(i-beg)/2-1+beg])
    }
    if(array[i]>array[(i-beg)/2-1+beg])
    {
        SWAP(array[i],array[(i-beg)/2-1+beg])
    }
}
return 0;
}
/*排序*/
int HeapSort_2(int *array,int num)
{
int i;
for(i=0;i<num-1;i++)
{
   AdjustHeap_2(array,num,i);
}
return 0;
}
int test(int (*p)(int *,int),char *pr,int *array,int num)
{
printf("****************************堆排序函数%s ***************************\n",pr);
printf("排序前:\n");
myputs(array,num);
(*p)(array,num);
printf("排序后:\n");
myputs(array,num);
return 0;
}
int main()
{

int array1[]={ 
               3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
              35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
              30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
              53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,            
              };
              
int array2[]={ 
               3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
              35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
              30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
              53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,            
              };
              
test(HeapSort_1,"HeapSort_1",array1,sizeof(array1)/sizeof(int));
test(HeapSort_2,"HeapSort_2",array2,sizeof(array2)/sizeof(int));
return 0;
}

在这里插入图片描述

归并排序

#include<stdio.h>
#include<stdlib.h>
#define SWAP(X,Y) {X=X+Y;Y=X-Y;X=X-Y;}
/*整数数组输出函数*/
int myputs(int *array,int num)
{
int i;
for(i=0;i<num;i++)
    {
    printf("%3d ",array[i]);
    if(!((i+1)%20))printf("\n");
    }
printf("\n");
return 0;
}
/*归并排序(从小到大)*/
/*合并*/
int Merge(int *array,int beg1,int num1,int beg2,int num2)
{
    int *p=(int *)malloc((num1+num2)*sizeof(int));
    int *tmp=p;
    int i=beg1,j=beg2,k=0;
    
    while((i<(beg1+num1))&&(j<(beg2+num2)))
    {
        if(array[i]<=array[j])
            *tmp++=array[i++];
        else
            *tmp++=array[j++];
    }
    
    if((beg1+num1)==i)
    {
        while(j<(beg2+num2))
            *tmp++=array[j++];
    }
    else if((beg2+num2)==j)
    {
        while(i<(beg1+num1))
            *tmp++=array[i++];
    }
    
    for(k=0;k<num1+num2;k++)
        array[beg1+k]=p[k];
    
    free(p);   
    return 0;
}
/*归并排序*/
int MergeSort_1(int *array,int beg,int num)
{
    if(num>2)
    {
        MergeSort_1(array,beg,num/2);/*将数组前一半归并排序*/
        MergeSort_1(array,beg+num/2,num-num/2);/*将数组后一半归并排序*/
        Merge(array,beg,num/2,beg+num/2,num-num/2);/*将数组以上两半合并*/
    }
    else if(2==num)
    {
        if(array[beg]>array[beg+1])
        {
            SWAP(array[beg],array[beg+1])
        }
    }
return 0;
}

int test(int (*p)(int *,int,int),char *pr,int *array,int beg,int num)
{
printf("****************************归并排序函数%s ***************************\n",pr);
printf("排序前:\n");
myputs(array,num);
(*p)(array,beg,num);
printf("排序后:\n");
myputs(array,num);
return 0;
}

int main()
{
int array1[]={ 
               3, 5, 9,88, 7,53,87, 4,38, 1,65,75,46,25,93, 6, 2,79,12, 4,
              35,59,98,87,75,58,84,43,31,16,67,74,42,29,96,62,27,71,14,43,
              30,50,90,88,70,35,78,40,83,10,56,57,46,52,93,60,20,97,21,40,
              53,95,89,78,57,85,48,34,13,61,76,47,24,92,69,26,72,17,41,34,            
              };  
test(MergeSort_1,"MergeSort_1",array1,0,sizeof(array1)/sizeof(int));
return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值