算法之堆的实现
来源:互联网 发布:购物软件哪个好 编辑:程序博客网 时间: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
- 算法之堆的实现
- 【排序算法】之堆排序的实现
- 算法设计之堆的实现与堆排序实现(C++实现)
- 算法外功修炼之二 堆排序的java实现
- 常见排序算法的C语言实现之堆排序
- 算法设计之,堆,堆排序,基于最大堆的最大优先队列的实现(C++实现)
- 建堆的算法实现
- Dijkstra算法的堆实现
- 堆排序算法的实现
- 堆排序算法的实现
- 堆排序算法的实现
- 堆排序的算法实现
- 堆排序算法的实现!!
- 《算法之美》---二叉堆及其实现
- 动手实现 算法 之 “堆排序”
- 堆排序算法之JAVA实现
- Java实现排序算法之堆排序
- java实现排序算法之堆排序
- To be g beast programmer
- 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
- java集合框架介绍
- Linux命令学习手册-iostat命令
- 项目启动tomcat访问不到主页被占用的解决方法
- 算法之堆的实现
- pta L2-013. 红色警报
- 在ubuntu14.04+ROS(indigo)下驱动并调试Basler(acA3800-14um)摄像头
- java中代理设计模式和适配器模式
- kerkee 在 Android 上的快速上手指南
- 461. Hamming Distance
- JS 实现审核流程状态的动态显示
- 【最大子段和】SDUT 3100 动态规划?
- ajax的traditional属性