堆排序算法实现(最小堆)

最小堆排序1

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
const int MAX = 0x3f3f3f3f;
const int maxn =  1000;
typedef struct HeapStruct *MinHeap;
struct HeapStruct {
    int data[maxn];
    int Size;
    int Capacity;
};//堆

MinHeap Creat(int maxsize)//建最小堆
{
    MinHeap H =(MinHeap)malloc(sizeof(struct HeapStruct));
    H->Size=0;
    H->Capacity=maxsize;
    H->data[0]=-MAX;
    return H;
}

int IsFull(MinHeap H)//判断堆是否为满
{
    if(H->Size==H->Capacity)return 1;
    else return 0;
}

int IsEmpty(MinHeap H)//判断堆是否为空
{
    if(H->Size != 0 )return 0;
    else return 1;
}

void Insert(MinHeap H ,int x)//插入
{
    int i;
    if(IsFull(H)){
        printf("It is full");
        return ;
    }
    i=++H->Size;
    for (;H->data[i/2]>x;i/=2){
        H->data[i]=H->data[i/2];
    }
    H->data[i]=x;
}

int DelectMin(MinHeap &H)//取出
{
    int p,c,minitem,temp;
    if (IsEmpty(H)){
        printf("It is empty");
        return 0;
    }
    minitem=H->data[1];
    temp=H->data[H->Size--];
    for (p=1;p*2<=H->Size;p=c)
    {
        c=p*2;
        if((c!=H->Size)&&(H->data[c]>H->data[c+1]))
            c++;
        if (temp<=H->data[c])break;
        else H->data[p]=H->data[c];
    }
    H->data[p]=temp;
    return minitem;
}
int main(){
    int n,s,sum=1;
    // freopen("C:/Users/111/Desktop/text/input.txt", "r", stdin);
	// freopen("C:/Users/111/Desktop/text/output.txt", "w", stdout);
    while(~scanf("%d",&n),n!=-1){
        int a[maxn];
        MinHeap H;
        H=Creat(maxn);
        for (int i = 0 ; i < n ; ++i){
            scanf("%d",&s);
            Insert(H,s);
        }
        printf("Case%d:%d\n",sum,n);
        for (int i = 0 ; i < n ; ++i){
             printf("%d ",DelectMin(H));
        }
        printf("\n");
        sum++;
    }
    // fclose(stdin);//关闭重定向输入
	// fclose(stdout);//关闭重定向输出 
    return 0;
}

最小堆排序2

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
const int MAX = 0x3f3f3f3f;
const int maxn =  1000;
typedef struct HeapStruct *MinHeap;
struct HeapStruct {
    int data[maxn];
    int Size;
    int Capacity;
};//堆

MinHeap Creat(int maxsize)//建堆
{
    MinHeap H =(MinHeap)malloc(sizeof(struct HeapStruct));
    H->Size=0;
    H->Capacity=maxsize;
    H->data[0]=-MAX;
    return H;
}

int IsFull(MinHeap H)//判断堆是否为满
{
    if(H->Size==H->Capacity)return 1;
    else return 0;
}

int IsEmpty(MinHeap H)//判断堆是否为空
{
    if(H->Size != 0 )return 0;
    else return 1;
}

void Insert(MinHeap H ,int x)//插入
{
    int i;
    if(IsFull(H)){
        printf("It is full");
        return ;
    }
    i=++H->Size;
    H->data[i]=x;
}

void PercDown(MinHeap H ,int i)//下滤
{
    int p,c;
    int x;
    x=H->data[i];
    for (p=i;p*2<=H->Size;p=c){
        c=p*2;
        if(c!=H->Size&&H->data[c]>H->data[c+1])
            c++;
        if(H->data[c]>=x)break;
        else H->data[p]=H->data[c];
    }
    H->data[p]=x;
}

void buildheap(MinHeap H)//建最小堆
{
    int i;
    for (i=H->Size/2;i>0;i--){
        PercDown(H,i);
    }
}

int DelectMin(MinHeap &H)//取出
{
    int p,c,minitem,temp;
    if (IsEmpty(H)){
        printf("It is empty");
        return 0;
    }
    minitem=H->data[1];
    temp=H->data[H->Size--];
    for (p=1;p*2<=H->Size;p=c)
    {
        c=p*2;
        if((c!=H->Size)&&(H->data[c]>H->data[c+1]))
            c++;
        if (temp<=H->data[c])break;
        else H->data[p]=H->data[c];
    }
    H->data[p]=temp;
    return minitem;
}
int main(){
    int n,s,sum=1;
    // freopen("E:/c++/input.txt", "r", stdin);
	// freopen("E:/c++/output.txt", "w", stdout);
    while(~scanf("%d",&n),n!=-1){
        int a[maxn];
        MinHeap H;
        H=Creat(maxn);
        for (int i = 0 ; i < n ; ++i){
            scanf("%d",&s);
            Insert(H,s);
        }
        buildheap(H);
        printf("Case%d:%d\n",sum,n);
        for (int i = 0 ; i < n ; ++i){
             printf("%d ",DelectMin(H));
        }
        printf("\n");
        sum++;
    }
    // fclose(stdin);//关闭重定向输入
	// fclose(stdout);//关闭重定向输出 
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值