现在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. Docker实践之03-Dockerfile指令详解

    目录 FROM,指定基础镜像 RUN,执行命令 COPY,复制文件 ADD,复制并解压文件 CMD,容器启动命令 ENTRYPOINT,入口点 ENV,设置环境变量 ARG,构建参数 VOLUME,定 ...

  2. java的特性与优势

    java的特性与优势 简单性 面向对象 可移植性 高性能 分布式 动态性 多线程 安全性 健壮性

  3. py库:pdfminer3k、docx。(PDFf转word)

    安装pdfminer模块: pip install pdfminer3k 安装docx模块: https://www.lfd.uci.edu/~gohlke/pythonlibs/  下载 pytho ...

  4. c#动态类转json,再由json转xml

    直接上代码了,多说无意了. using System; using System.Collections; using System.Collections.Generic; using System ...

  5. c++递归函数

    一.什么是递归算法 递归即递推+回归.递归算法是把问题转化为规模缩小了的同类子问题,然后递归调用函数(或过程)来表示问题的解. 二.递归算法的特点 1.必须有 递归函数 + 递归出口 2.递归算法解题 ...

  6. go语言学习---使用os.Args获取简单参数(命令行解析)

    实例1: //main package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args ...

  7. Disruptor分布式id生成策略

    需要的pom文件: <!-- 顺序UUID --> <dependency> <groupId>com.fasterxml.uuid</groupId> ...

  8. CAS 5.x搭建常见问题系列(1).未认证的授权服务

    错误内容 错误信息如下: 未认证授权的服务 CAS的服务记录是空的,没有定义服务.希望通过CAS进行认证的应用程序必须在服务记录中明确定义 错误原因 CAS 5.x 默认情况下不支持HTTP的客户端接 ...

  9. js注意点

    1.在JS中:var a=''; 则 a==0或a==false 结果都为true;  如果是“====” 则为false

  10. aspnetcore 容器化部属到阿里云全过程记录

    第一次写博客,作为一个全栈er,记录一下从阿里云到产品运维上线的全过程 一.阿里云上的设置 购买阿里云ECS后: 进控制台查看实例公网IP 在控制台.网络与安全->安全组,配置规则 点击进去可以 ...