算法之堆的实现

来源:互联网 发布:购物软件哪个好 编辑:程序博客网 时间:2024/05/16 06:22

头文件 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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 w7电脑中病毒了怎么办 电脑中病毒了该怎么办 泰迪呼吸急促怎么办啊 狗狗呼吸急促是怎么办 狗狗着凉了呕吐怎么办 狗鼻子流黄鼻涕怎么办 刚出生婴儿睡觉不踏实怎么办 有人溺水后你该怎么办 借钱不还怎么办没欠条 私人欠货款不还怎么办 公司欠货款不还怎么办 两个人离婚一方不同意怎么办 比亚迪l3油耗高怎么办 u盘密码忘记了怎么办 主板没有m.2接口怎么办 点痣留下了疤怎么办 危险三角区长痘痘怎么办 挤了危险三角区怎么办 三角区长痘挤了怎么办 三角区发红长痘怎么办 激光祛斑碰水了怎么办 激光打痣留下坑怎么办 点痣之后留下坑怎么办 去痣留下的红印怎么办 激光点痦子留疤怎么办 激光点痣的疤痕怎么办 做完眉毛碰水了怎么办 脸上疤掉了有坑怎么办 结痂不小心抠掉怎么办 脸上肉松弛怎么办19岁 点痣留下来的疤怎么办 激光祛斑的红印怎么办 脸上疤掉了红印怎么办 痘痘发炎了红肿怎么办 脸上的斑越来越多了怎么办 点痣留下的疤痕怎么办 额头又高又大怎么办 脸太长额头太高怎么办 动车因台风停运怎么办 爸妈50了要离婚怎么办 鸿利彩票黑了钱怎么办