算法之堆的实现

头文件 Heap.c

#ifndef _HEAP_H_
#define _HEAP_H_

#include <stdio.h>
#include "time.h"
#include "stdlib.h"
#include "ASSERT.h"
#include "arithmetic.h"

typedef struct{

    int *data;
    int heapMaxIndex;
    int count;
}Heap;
    void swap(int *first,int *last);
    Heap maxHeap(Heap heap,int capacity);
    void maxHeapifyArr(Heap *heap,int arr[],int capacity);
    Heap heapInsert(Heap heap,int item);
    int heapSize(Heap heap);
    int heapExtractMax(Heap *heap);

#endif

c代码 Heap.c

#include "Heap.h"
/*********************************************
*函数名:堆的实现
*********************************************/
//根据对的容量生成一个最大堆
Heap maxHeap(Heap heap,int capacity){
    heap.heapMaxIndex = capacity;
    heap.count = 0;
    heap.data = (int *)malloc(sizeof(int)*(capacity+1));//根据heap的容量为指针分配内存空间

    return heap;
}


//返回堆的大小
int heapSize(Heap heap){
    return heap.count;
}
//判断堆是否为空
int heapIsEmpty(Heap heap){
    return heap.count == 0;
}
Heap heapShiftUp(Heap heap,int k){
    //将插入的元素与父元素比较大小,如果小则交换位置
    while( k > 1 && *(heap.data+k/2) < *(heap.data+k) ){
        swap((heap.data+k/2),(heap.data+k));
        k /= 2;
    }
    return heap;
}


//向堆中插入元素
Heap heapInsert(Heap heap,int item){
    if(heap.count + 1 <= heap.heapMaxIndex){
       *(heap.data+heap.count +1) = item;
        heap.count ++;
        return(heapShiftUp(heap,heap.count));
    }
    else{
        return heap;
    }
}
void heapShiftDown(Heap *heap,int k){

    while(2*k <= heap->count){      //判断下左枝是否存在
        int j = 2*k;                //在此循环中,data[k]和data[j]交换
        if(j+1 <= heap->count){     //判断下右枝是否存在
            if(*(heap->data+j+1) > *(heap->data+j)){
                j += 1;
            }
        }
        if(*(heap->data+k) >= *(heap->data+j))
            break;

        swap(heap->data+k,heap->data+j);
        k = j;
    }

}
//取出堆中最大的元素
int heapExtractMax(Heap *heap){
    assert(heap->count > 0);
    int max = *(heap->data + 1);;
    swap((heap->data+1),(heap->data+heap->count));
    heap->count --;
    heapShiftDown(heap,1);
    return max;
}
//释放为堆中为指针分配的空间
void heapFree(Heap heap){
    free(heap.data);
}
//Heapify算法将数组插入到堆中
void maxHeapifyArr(Heap *heap,int arr[],int capacity){
    heap->heapMaxIndex = capacity;
    heap->data = (int *)malloc(sizeof(int)*(capacity+1));
    int i;
    for(i=0;i<capacity;i++){
        *(heap->data+i+1) = arr[i];
    }
    heap->count = capacity;

    for(i=heap->count/2;i>=1;i--){
        heapShiftDown(heap,i);
    }
}


/*********************************************
*函数名:原地堆排序,不占用空间
*********************************************/
void __shiftDown(int arr[],int n,int k){
    while(2*k+1 < n){
        int j = 2*k+1;
        if(j+1 < n && arr[j+1] > arr[j]){
            j += 1;
        }
        if(arr[k] >= arr[k])
            break;
        swap(&arr[k],&arr[j]);
        k=j;
    }
}
void heapSort(int arr[],int n){
    int i;
    //heapfiy,将数组使用heapfiy进行排序
    for(i=(n-1)/2;i>=0;i--){
        __shiftDown(arr,n,i);
    }
    for(i=n-1;i>0;i--){
        //将第一个元素放到末尾,把其他的元素进行最大堆排序
        swap(&arr[0],&arr[i]);
        __shiftDown(arr,i,0);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值