现在po一下C语言版本的,留作以后接口使用.
1 #include <stdio.h>
#include <stdlib.h> #define HEAP_SIZE 100
#define HEAP_FULL_VALUE -100 #if 0
/*小顶堆存储结构*/
typedef struct small_heap
{
int data[HEAP_SIZE];
int num;
}SMALL_HEAP;
#endif /*
* name: heap_Swap
*
* purpose:
* swap two value of heap
*/
static void heap_Swap(int heap[],int index_src,int index_dst)
{
int tmp = heap[index_src]; heap[index_src] = heap[index_dst];
heap[index_dst] = tmp;
} /*
* name: heap_Up
*
* purpose:
* move up value of the index position to adjust heap struct
*/
static void heap_Up(int heap[],int index)
{
int parent = index / ; while(parent >= )
{
if(heap[index] < heap[parent])
{
heap_Swap(heap,index,parent);
index = parent;
}
else
{
break;
}
}
} /*
* name: heap_Down
*
* purpose:
* move down value of the index position to adjust heap struct
*/
static void heap_Down(int heap[],int index,int heap_data_num)
{
if(index * > heap_data_num)
{//leaf node can not move down
return;
} while(index * <= heap_data_num )
{
int child = index * ; // left child if(child > heap_data_num)
{
return;
} if(index * < heap_data_num)
{//the node have two child
if(heap[child + ] < heap[child])
{
child += ; //right child is smaller update
} } if(heap[child] < heap[index])
{//the child samller than index swap value
heap_Swap(heap,index,child);
index = child;
}
else
{
break;
}
}
} /*
* name: heap_Insert
*
* purpose:
* insert a value into heap and ajust heap struct
*/
void heap_Insert(int heap[],int *heap_data_num,int value)
{
if(*heap_data_num == )
{
heap[] = HEAP_FULL_VALUE; //data 0 do not save in the heap
} (*heap_data_num)++; //update heap size
heap[*heap_data_num] = value; //add value to heap heap_Up(heap,*heap_data_num); //adjust heap struct
} /*
* name: heap_Delete
*
* purpose:
* delete a value from heap
*/
void heap_Delete(int heap[],int *heap_data_num,int value)
{
int index; for(index = ; index <= *heap_data_num; index++)
{
if(heap[index] == value)
{
break;
}
} if(index > *heap_data_num)
{//the value is not exist
return;
} heap[index] = heap[*heap_data_num]; //set the index value as final value (*heap_data_num)--;//the final value is not as the heap heap_Down(heap,index,*heap_data_num); //move down int parent = index / ;
if(parent > && heap[index] < heap[parent])
{//ajust to the special situation
heap_Up(heap,index);
} heap[*heap_data_num + ] = HEAP_FULL_VALUE; //delete final data
} /*
* name:heap_Init_Adjust
*
* purpose:
* to adjust the init heap
* time complex is O(n) better than insert value one by one
*
* explain:
* because the heap is a complete binary tree so just first half of heap data is not leaf node
*
*/
void heap_Init_Adjust(int heap[],int heap_data_num)
{
int index; for(index = heap_data_num / ; index >= ; index--)
{
heap_Down(heap,index,heap_data_num); //adjust the heap struct at index position
}
} void heap_Print(int heap[],int heap_data_num);
/*
* name: heap_Sort
*
* purpose:
* to sort the heap to a descending order
*/
void heap_Sort(int heap[],int heap_data_num)
{
int index; for(index = heap_data_num; index > ; index--)
{
heap_Swap(heap,,index); //swap the heap top with the final value
heap_Down(heap,,index - ); //to ajust the heap struct afert sort one value
//heap_Print(heap,heap_data_num);
}
} /*
* name: heap_Print
*
* purpose:
* print heap data as commen order
*/
void heap_Print(int heap[],int heap_data_num)
{
int i;
for(i = ; i <= heap_data_num; i++)
{
printf("%d ",heap[i]);
} printf("\n");
} int main()
{
int heap[HEAP_SIZE];
int i,heap_data_num = ; for(i = ; i < heap_data_num; i++)
{
heap[i] = HEAP_FULL_VALUE;
} #if 1
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,); heap_Print(heap,heap_data_num); heap_Sort(heap,heap_data_num);
heap_Print(heap,heap_data_num); heap_Init_Adjust(heap,heap_data_num);
heap_Print(heap,heap_data_num); heap_Sort(heap,heap_data_num);
heap_Print(heap,heap_data_num); heap_Delete(heap,&heap_data_num,);
heap_Print(heap,heap_data_num);
heap_Delete(heap,&heap_data_num,);
heap_Print(heap,heap_data_num); heap_Sort(heap,heap_data_num);
heap_Print(heap,heap_data_num); heap_Init_Adjust(heap,heap_data_num);
heap_Print(heap,heap_data_num); #endif #if 0
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,);
heap_Insert(heap,&heap_data_num,); heap_Print(heap,heap_data_num); heap_Delete(heap,&heap_data_num,); heap_Print(heap,heap_data_num);
#endif }

小顶堆第二弹-----堆降序排序(C语言非递归)的更多相关文章

  1. mysql大于当前时间置顶并按升序排序,小于当前时间的置尾并按降序排序

    现在用id来代替时间这样好测试 看一下测试表数据 执行按需求规则排序的sql SELECT * FROM number_generator ORDER BY id < 16 , IF(id &l ...

  2. R_Studio(关联)使用apriori函数简单查看数据存在多少条关联规则,并按支持度降序排序输出

    查看数据menu_orders.txt文件存在多少条关联规则,并按支持度降序排序输出 #导入arules包 install.packages("arules") library ( ...

  3. 怎么实现元素ol的降序排序显示

    首先介绍一下什么是ol元素.这里直接引用MDN里面的定义:The HTML <ol> Element (or HTML Ordered List Element) represents a ...

  4. c++ sort降序排序

    sort是c++ STL中提供的一个函数模板,可以用来对多种类型进行排序. 默认是升序排序.它有两种使用方法: default (1) template <class RandomAccessI ...

  5. LINQ中的OrderBy实现按照两个字段升序、降序排序操作

    在公司或许有这种需求,先根据第一个某个字段按照升序排序,然后如果相同,在按照第二个某个字降序排序,我们该怎么去实现呢? 现在来教教大家分别使用Labmda和LINQ进行这种操作. 1.先按照第一个字段 ...

  6. 现在输入 n 个数字, 以逗号, 分开; 然后可选择升或者 降序排序;

    /* 现在输入 n 个数字, 以逗号, 分开: 然后可选择升或者 降序排序: */ import java.util.*; public class bycomma{ public static St ...

  7. java数组降序排序之冒泡排序

    import java.util.Arrays;//必须加载 class Demo{ public static void main(String []args){ int[] arr={3,54,4 ...

  8. 用shell处理以下内容 1、按单词出现频率降序排序! 2、按字母出现频率降序排序! the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support

    此题目有多种解法,sed.awk.tr等等,都可以解决此题,命令运用灵活多变. 编写shell脚本no_20.sh 解法1: #!/bin/bash ###-------------CopyRight ...

  9. 通过orderby关键字,LINQ可以实现升序和降序排序。LINQ还支持次要排序。

    通过orderby关键字,LINQ可以实现升序和降序排序.LINQ还支持次要排序. LINQ默认的排序是升序排序,如果你想使用降序排序,就要使用descending关键字. static void M ...

随机推荐

  1. 映美FP-530K+打印发票的各种经验

    本人虽然写了很多lodop博文,但是程序开发一般都是用虚拟打印机测试,实际打印机打印中还可能存在各种问题,毕竟理论都不如实践能获得更多的打印经验.当个人使用过的打印机不多,只有映美FP-530K+,用 ...

  2. Excel统计发票和金税盘核对新版

    之前的博文:如何使用Excel表格状态栏动态查看统计,介绍了如何利用excel一拉就可以进行统计,和金税盘的月度统计统计.由于最近年月日显示成方框,所以作废了发票和对冲了上月的一张发票,导致这个月出现 ...

  3. 【tensorflow基础】tensorflow中 tf.reduce_mean函数

    参考 1. tensorflow中 tf.reduce_mean函数: 完

  4. Java之安装JDK

    因为Java程序必须运行在JVM只是,所以我们第一件事情就是安装JDK 从Oracle官网下载最新稳定版JDK 一,Linux系统CentOS安装JDK 下载rpm安装包 安装 rpm -ivh jd ...

  5. .net MVC 项目中 上传或者处理进度获取方案

    首先讲下思路 就是利用js轮询定时的给后台发送数据 话不多说看代码 --------- 以下是相关方法 var t function timedCount() { $.ajax({ type: 'ge ...

  6. consul集群搭建以及ACL配置

    由于时间匆忙,要是有什么地方没有写对的,请大佬指正,谢谢.文章有点水,大佬勿喷这篇博客不回去深度的讲解consul中的一些知识,主要分享的我在使用的时候的一些操作和遇见的问题以及解决办法.当然有些东西 ...

  7. 下一代无服务器的发展形态: Serverless2.0

    6 月 25 日,在上海召开的 KubeCon 2019 大会上,腾讯云重磅发布了下一代无服务器的发展形态:Serverless2.0.本文将以 Serverless 的概念.发展.形态.应用以及技术 ...

  8. pyhton数据类型:字典、集合、列表、元组

    基本常识 元组 列表 字典 集合 初始化 tuple=(1,2,3,4) list=[1,2,3,4] dic={'a':12,'b':34} set={1,2,3,4} 元素索引 tuple[0] ...

  9. [转帖]JAVA BIO与NIO、AIO的区别(这个容易理解)

    JAVA BIO与NIO.AIO的区别(这个容易理解) https://blog.csdn.net/ty497122758/article/details/78979302 2018-01-05 11 ...

  10. (一)线性表(linear list)

    文章目录 定义 特点 ADT (abstract data type) 定义 摘抄自 维基百科 线性表(英语:Linear List)是由 n(n≥0)个 数据元素(结点)a[0],a[1],a[2] ...