heap相关算法的简单实现
// 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相关算法的简单实现的更多相关文章
- 【STL学习】堆相关算法详解与C++编程实现(Heap)
转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介 堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...
- 机器学习&数据挖掘笔记(常见面试之机器学习算法思想简单梳理)
机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 前言: 找工作时( ...
- [转]机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理) 转自http://www.cnblogs.com/tornadomeet/p/3395593.html 前言: 找工作时(I ...
- [java,2017-05-15] 内存回收 (流程、时间、对象、相关算法)
内存回收的流程 java的垃圾回收分为三个区域新生代.老年代. 永久代 一个对象实例化时 先去看伊甸园有没有足够的空间:如果有 不进行垃圾回收 ,对象直接在伊甸园存储:如果伊甸园内存已满,会进行一次m ...
- UCI机器学习库和一些相关算法(转载)
UCI机器学习库和一些相关算法 各种机器学习任务的顶级结果(论文)汇总 https://github.com//RedditSota/state-of-the-art-result-for-machi ...
- 采样方法(二)MCMC相关算法介绍及代码实现
采样方法(二)MCMC相关算法介绍及代码实现 2017-12-30 15:32:14 Dark_Scope 阅读数 10509更多 分类专栏: 机器学习 版权声明:本文为博主原创文章,遵循CC 4 ...
- 盘点十大GIS相关算法
1.道格拉斯-普克算法(Douglas–Peucker) 道格拉斯-普克算法(Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法.迭代适应点算法.分裂与合并算法)是将曲 ...
- 带有两个输入字段和相关标记的简单 HTML 表单:
带有两个输入字段和相关标记的简单 HTML 表单: 意思就是说Male 和id="male"绑定在一起. <html> <body> <p> ...
- Linux IO调度器相关算法介绍(转)
IO调度器(IO Scheduler)是操作系统用来决定块设备上IO操作提交顺序的方法.存在的目的有两个,一是提高IO吞吐量,二是降低IO响应时间.然而IO吞吐量和IO响应时间往往是矛盾的,为了尽量平 ...
随机推荐
- es6 模块与commonJS的区别
在刚接触模块化开发的阶段,我总是容易将export.import.require等语法给弄混,今天索性记个笔记,将ES6 模块知识点理清楚 未接触ES6 模块时,模块开发方案常见的有CommonJS. ...
- iOS刨根问底-深入理解GCD
概述 做过iOS开发的同学相信对于GCD(Grand Central Dispatch)并不陌生,因为在平时多线程开发过程中GCD应该是使用最多的技术甚至它要比它的上层封装NSOperation还要常 ...
- 执行ArrayList的remove(object)方法抛异常?
简介 或许有很多小伙伴都尝试过如下的代码: ArrayList<Object> list = ...; for (Object object : list) { if (条件成立) { l ...
- HTB::Postman
实验环境 渗透过程 0x01 信息搜集 masscan扫描 扫描结果目标服务开放了22(ssh),80(http),6379(redis),10000(webmin)端口 nmap扫描 nmap -s ...
- VirtualBox安装centos8
原文链接:https://www.wjcms.net/archives/vagrantbox安装centos8 VirtualBox安装centos8 在线安装(不推荐) 打开git软件,直接运行命令 ...
- shell专题(二):Shell解析器
(1)Linux提供的Shell解析器有: [atguigu@hadoop101 ~]$ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/da ...
- POJ 1063 Flip and Shift 最详细的解题报告
题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3 ...
- pytest框架的安装与使用
pytest框架的安装与使用 一,pytest了解 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高. ...
- 【C#】WebService接受跨域请求及返回json数据
问题概述 通过Web Service发布服务供客户端调用是一种非常简单.方便.快速的手段,并且服务发布后会有一个服务说明页面,直观明了,如图: 一般情况下,在web页面中的JavaScript中调用W ...
- 008.Nginx静态资源
一 Nginx静态资源概述 1.1 静态资源类型 Nginx作为静态资源Web服务器部署配置, 传输非常高效, 常常用于静态资源处理,请求以及动静分离.通常非服务器动态运行生成的文件属于静态资源. 类 ...