Heapify
Given an integer array, heapify it into a min-heap array.
- public class Solution {
- /**
- * @param A: Given an integer array
- * @return: void
- */
- public void heapify(int[] array) {
- int heapSize = array.length;
- for (int i = heapSize / - ; i >= ; i--) {
- minHeapify(array, i, array.length);
- }
- }
- /// MaxHeapify is to build the max heap from the 'position'
- public void minHeapify(int[] array, int position, int heapSize)
- {
- int left = left(position);
- int right = right(position);
- int minPosition = position;
- if (left < heapSize && array[left] < array[position]) {
- minPosition = left;
- }
- if (right < heapSize && array[right] < array[minPosition]) {
- minPosition = right;
- }
- if (position != minPosition) {
- swap(position, minPosition, array);
- minHeapify(array, minPosition, heapSize);
- }
- }
- public void swap(int i, int j, int[] A) {
- int temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- /// return the left child position
- public int left(int i)
- {
- return * i + ;
- }
- /// return the right child position
- public int right(int i)
- {
- return * i + ;
- }
- }
Heapify的更多相关文章
- LintCode "Heapify"
My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...
- Heap和Heapify
最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQ ...
- Lintcode: Heapify && Summary: Heap
Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of he ...
- 两种建立堆的方法HeapInsert & Heapify
参考 堆排序中两种建堆方法的比较 第一种方法HeapInsert 它可以假定我们事先不知道有多少个元素,通过不断往堆里面插入元素进行调整来构建堆. 它的大致步骤如下: 首先增加堆的长度,在最末尾的地方 ...
- 为什么堆化 heapify() 只用 O(n) 就做到了?
heapify() 前面两篇文章介绍了什么是堆以及堆的两个基本操作,但其实呢,堆还有一个大名鼎鼎的非常重要的操作,就是 heapify() 了,它是一个很神奇的操作, 可以用 O(n) 的时间把一个乱 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 《徐徐道来话Java》:PriorityQueue和最小堆
在讲解PriorityQueue之前,需要先熟悉一个有序数据结构:最小堆. 最小堆是一种经过排序的完全二叉树,其中任一非终端节点数值均不大于其左孩子和右孩子节点的值. 可以得出结论,如果一棵二叉树满足 ...
- 计算机程序的思维逻辑 (46) - 剖析PriorityQueue
上节介绍了堆的基本概念和算法,本节我们来探讨堆在Java中的具体实现类 - PriorityQueue. 我们先从基本概念谈起,然后介绍其用法,接着分析实现代码,最后总结分析其特点. 基本概念 顾名思 ...
- codevs 2830 蓬莱山辉夜
2830 蓬莱山辉夜 http://codevs.cn/problem/2830/ 题目描述 Description 在幻想乡中,蓬莱山辉夜是月球公主,居住在永远亭上,二次设定说她成天宅在家里玩电脑, ...
随机推荐
- LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS
LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163. ...
- oracle (+) 什么意思
oracle中的(+)是一种特殊的用法,(+)表示外连接,并且总是放在非主表的一方. 例如左外连接:select A.a,B.a from A LEFT JOIN B ON A.b=B.b;等价于se ...
- Leetcode题库——49.字母异位词分组【##】
@author: ZZQ @software: PyCharm @file: leetcode49_groupAnagrams.py @time: 2018/11/19 13:18 要求:给定一个字符 ...
- Git(2.14.1版本)学习及使用(一)
OuZeBo原创作品.转载请注明出处 http://www.cnblogs.com/OuZeBo/p/7477465.html 1.下载git:https://git-scm.com/ 2.安装(本人 ...
- RYU 灭龙战 fourth day (2)
RYU 灭龙战 fourth day (2) 前言 之前试过在ODL调用他们的rest api,一直想自己写一个基于ODL的rest api,结果还是无果而终.这个小目标却在RYU身上实现了.今日说法 ...
- time since epoch
C++11 提供了新的获取系统时间的库函数,在获取时间的时候一般常用的是获取time since epoch,下面来看一下如何获取这个时间. #include <iostream> #in ...
- 什么是Asp.net Core?和 .net core有什么区别?
为什么要写这篇文章 写这篇文章有两个原因,第一个是因为新站点创建出来后一直空置着,所以写一篇文章放在这里.第二就是因为近来在做一些基于Asp.net core平台的项目开发,也遇到了一些问题,正好趁此 ...
- QEMU简单知识 以及磁盘格式转换的简单命令
From 百度百科 QEMU,是由 Fabrice Bellard开发的通用.开源机器模拟与虚拟化软件,Fabrice Bellard是另一个著名的C编译器的作者.QEMU可以在不同的机器上运行独自开 ...
- C#小技巧
1.将字符串转换成大写ToUpper string a="zxc"; a.ToUpper() 输出结果ZXC; a.ToUpper().Contains("Z" ...
- Several ports (8005, 8080, 8009) required by Tomcat
转载:http://blog.csdn.net/tomoto_zh/article/details/51931945 先找到Java项目中 Servers找到Server.xml然后 把8005, ...