// 12:06 PM/09/28/2017
#pragma once
//向下调整算法 主要用来make_heap 以及pop_heap
inline void adjustDown(int* heap, const int first, const int last)
{
if (last == first + )return;
int value = heap[first];
int hole = first;//当前要调整的节点
int childHole = hole * ;//其左子女节点
while (childHole < last)//当左子女节点存
{
if (childHole < last - && heap[childHole] < heap[childHole + ])//如果左右子女都存在
childHole++;
if (value > heap[childHole]) break; {
//当待调整元素 比子女元素小是,将子女元素移动到父节点
heap[hole] = heap[childHole];
hole = childHole;//将该子女元素设置为洞
childHole = hole * ;
}
}
heap[hole] = value;
} //像上调整 实际上也就是 pop_heap
inline void adjustUp(int* heap, const int first, const int last)
{
if (last == first + )return;
int hole = last - ;//最后一个元素设置为洞
int parentHole = hole / ;//该元素的父节点
int value = heap[last - ];//当前要调整元素
while (parentHole >= first && value > heap[parentHole])//父节点存在,且当前值大于父节点
{
heap[hole] = heap[parentHole];//父节点的值给当前洞
hole = parentHole;//父节点设置为洞
parentHole = hole / ;
}
heap[hole] = value;
} inline void push_heap(int* heap, const int firstIndex, const int lastIndex)
{
adjustUp(heap, firstIndex, lastIndex);
} //弹出最大元素
inline void pop_head(int* heap, const int firstIndex, const int lastIndex)
{
int value = heap[firstIndex];//要弹出的元素
heap[firstIndex] = heap[lastIndex - ];//把最后一个元素给要弹出的
heap[lastIndex - ] = value;//要弹出的放到最后
adjustDown(heap, firstIndex, lastIndex - );//进行向下调整
} inline void sort_heap(int* heap, int firstIndex, int lastIndex)
{
int curLast = lastIndex;
while (curLast > firstIndex)
{
pop_head(heap, firstIndex, curLast);
curLast--;
}
} inline void make_heap(int* heap, int firstIndex, int lastIndex)
{
int hole = (lastIndex - ) / ;
while (hole >= firstIndex)
{
adjustDown(heap, hole, lastIndex);
hole--;
}
} inline void partial_sort(int* heap, const int first, const int middle, const int last)
{
int i = middle;
make_heap(heap, first, middle);
while (i < last)
{
if (heap[i] < heap[first])
{
int value = heap[first];
heap[first] = heap[i];
heap[i] = value;
adjustDown(heap, first, middle);
}
i++;
}
sort_heap(heap, first, middle);
}

heap相关算法的简单实现的更多相关文章

  1. 【STL学习】堆相关算法详解与C++编程实现(Heap)

    转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介   堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...

  2. 机器学习&数据挖掘笔记(常见面试之机器学习算法思想简单梳理)

    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 前言: 找工作时( ...

  3. [转]机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)

    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 转自http://www.cnblogs.com/tornadomeet/p/3395593.html 前言: 找工作时(I ...

  4. [java,2017-05-15] 内存回收 (流程、时间、对象、相关算法)

    内存回收的流程 java的垃圾回收分为三个区域新生代.老年代. 永久代 一个对象实例化时 先去看伊甸园有没有足够的空间:如果有 不进行垃圾回收 ,对象直接在伊甸园存储:如果伊甸园内存已满,会进行一次m ...

  5. UCI机器学习库和一些相关算法(转载)

    UCI机器学习库和一些相关算法 各种机器学习任务的顶级结果(论文)汇总 https://github.com//RedditSota/state-of-the-art-result-for-machi ...

  6. 采样方法(二)MCMC相关算法介绍及代码实现

    采样方法(二)MCMC相关算法介绍及代码实现 2017-12-30 15:32:14 Dark_Scope 阅读数 10509更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4 ...

  7. 盘点十大GIS相关算法

    1.道格拉斯-普克算法(Douglas–Peucker) 道格拉斯-普克算法(Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法.迭代适应点算法.分裂与合并算法)是将曲 ...

  8. 带有两个输入字段和相关标记的简单 HTML 表单:

    带有两个输入字段和相关标记的简单 HTML 表单: 意思就是说Male 和id="male"绑定在一起. <html> <body>   <p> ...

  9. Linux IO调度器相关算法介绍(转)

    IO调度器(IO Scheduler)是操作系统用来决定块设备上IO操作提交顺序的方法.存在的目的有两个,一是提高IO吞吐量,二是降低IO响应时间.然而IO吞吐量和IO响应时间往往是矛盾的,为了尽量平 ...

随机推荐

  1. [HCTF 2018]admin

    前言: 最近在BUUCTF刷题,参照师傅们wp后复现一下 0x01 拿到题目后进去如下界面 发现有登录和注册界面,相比是要登录后才能查看想要的信息. 查看页面源代码,看看有没有上面提示,界面如下 提示 ...

  2. Mysql基础(六):索引、数据库备份、锁和事务、慢查询优化、索引命中相关

    目录 数据库05 /索引.数据库备份.锁和事务.慢查询优化.索引命中相关 1. 什么是索引 2. 索引的原理 3. 索引的数据结构(聚集索引.辅助索引) 4. 索引操作 5. 索引的两大类型hash与 ...

  3. MYSQL 之 JDBC(五): 增删改查(三)PreparedStatement

    是Statement的子接口,可以传入带占位符的sql语句,并且提供了补充占位符变量的方法. 使用Statement需要进行拼写SQL语句,很辛苦,很容易出错. 引号的问题处理很复杂,不利于维护. 可 ...

  4. drf源码剖析系列(系列目录)

    drf源码剖析系列(系列目录) 01 drf源码剖析之restful规范 02 drf源码剖析之快速了解drf 03 drf源码剖析之视图 04 drf源码剖析之版本 05 drf源码剖析之认证 06 ...

  5. 机器学习实战基础(二十五):sklearn中的降维算法PCA和SVD(六) 重要接口,参数和属性总结

    到现在,我们已经完成了对PCA的讲解.我们讲解了重要参数参数n_components,svd_solver,random_state,讲解了三个重要属性:components_, explained_ ...

  6. hihoCoder 1039 字符消除 最详细的解题报告

    题目来源:字符消除 解题思路: 1.在给定字符串中的任意位置插入'A'.'B'.'C'中的任意一个字符,然后计算插入后的字符经过消除后最短的字符串长度: 2.在计算字符消除后最短长度时,智能一遍一遍的 ...

  7. Python爬虫学习02--pyinstaller

    Python爬虫学习02--打包exe可执行程序 1.上一次做了一个爬虫爬取电子书的Python程序,然后发现可以通过pyinstaller进行打包成exe可执行程序.发现非常简单好用 2.这是上次写 ...

  8. Ethical Hacking - Web Penetration Testing(13)

    OWASP ZAP(ZED ATTACK PROXY) Automatically find vulnerabilities in web applications. Free and easy to ...

  9. GPO - Set Date and Time for Updates

    For Windows Update, the limitation normally is a time window, disk space, network bandwidth. Create ...

  10. Ethical Hacking - NETWORK PENETRATION TESTING(5)

    Deauthentication Attacks Theory This attack is used to disconnect any device from any network within ...