现在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. 【python基础】python开启GPU加速

    前言 训练时使用GPU可以加速程序运行,本文介绍如何使用GPU加速. 前提条件 1. 机子有GPU显卡,并安装GPU显卡驱动: 2. 安装GPU的使用环境,CUDA等: 3. 打开nvidia-smi ...

  2. MongoDB的局域网连接问题

    问题前两天在本机连接虚拟机的MongoDB,总是连接拒绝 上网百度了一堆,找到一些看似解释,实则不一定管用的玩意. 自己找到一个解法是改etc/mongodb.conf文件,把bindIp的127.0 ...

  3. python:单元测试框架pytest的一个简单例子

    之前一般做自动化测试用的是unitest框架,发现pytest同样不错,写一个例子感受一下 test_sample.py import cx_Oracle import config from sen ...

  4. upgrade rubygems

    gem install rubygems-update update_rubygems gem update --system gem update

  5. Ansible-Hoc--样例

    一.常用场景 1. 列出支持的模块及模块功能说明: 2. sudo用法: 3. 检查服务器存活,复制本地文件到远程: 4. 多线程判断服务器的存活: 5.  显示所有主机的hostname: 6. 列 ...

  6. 「中山纪中集训省选组D1T1」最大收益 贪心

    题目描述 给出\(N\)件单位时间任务,对于第\(i\)件任务,如果要完成该任务,需要占用\([S_i, T_i]\)间的某个时刻,且完成后会有\(V_i\)的收益.求最大收益. 澄清:一个时刻只能做 ...

  7. Nvidia Jetson TX2开发板学习历程(1)- 详细开箱、上电过程

    考试周已经结束了,开发板也已经到了.希望借着这个假期能够好好的利用这块开发板学习Linux系统以及Tensorflow的相关知识. 我打算将学习历程通过博客的方式写出来,作为自己的笔记,也可以供以后拿 ...

  8. Golang_互斥锁

    为什么需要锁? 在并发的情况下,多个线程或协程同时去修改一个变量.使用锁能保证在某一时间点内,只有一个协程或线程修改这一变量. 锁的概念就是,我正在处理 a(锁定),你们等着,等我处理完了(解锁),你 ...

  9. crontab 定时删除

    /60 * * * /bin/find /usr/local/****/****/****/****/****.log.2019* -exec rm -f {} ; >/dev/null 2&g ...

  10. c# webapi 过滤器token、sign认证、访问日志

    1.token认证 服务端登录成功后分配token字符串.记录缓存服务器,可设置有效期 var token = Guid.NewGuid().ToString().Replace("-&qu ...