Given an integer array, heapify it into a min-heap array.

For a heap array A, A[0] is the root of heap, and for each A[i], A[i * 2 + 1] is the left child of A[i] and A[i * 2 + 2] is the right child of A[i].
 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的更多相关文章

  1. LintCode "Heapify"

    My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...

  2. Heap和Heapify

    最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQ ...

  3. 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 ...

  4. 两种建立堆的方法HeapInsert & Heapify

    参考 堆排序中两种建堆方法的比较 第一种方法HeapInsert 它可以假定我们事先不知道有多少个元素,通过不断往堆里面插入元素进行调整来构建堆. 它的大致步骤如下: 首先增加堆的长度,在最末尾的地方 ...

  5. 为什么堆化 heapify() 只用 O(n) 就做到了?

    heapify() 前面两篇文章介绍了什么是堆以及堆的两个基本操作,但其实呢,堆还有一个大名鼎鼎的非常重要的操作,就是 heapify() 了,它是一个很神奇的操作, 可以用 O(n) 的时间把一个乱 ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. 《徐徐道来话Java》:PriorityQueue和最小堆

    在讲解PriorityQueue之前,需要先熟悉一个有序数据结构:最小堆. 最小堆是一种经过排序的完全二叉树,其中任一非终端节点数值均不大于其左孩子和右孩子节点的值. 可以得出结论,如果一棵二叉树满足 ...

  8. 计算机程序的思维逻辑 (46) - 剖析PriorityQueue

    上节介绍了堆的基本概念和算法,本节我们来探讨堆在Java中的具体实现类 - PriorityQueue. 我们先从基本概念谈起,然后介绍其用法,接着分析实现代码,最后总结分析其特点. 基本概念 顾名思 ...

  9. codevs 2830 蓬莱山辉夜

    2830 蓬莱山辉夜 http://codevs.cn/problem/2830/ 题目描述 Description 在幻想乡中,蓬莱山辉夜是月球公主,居住在永远亭上,二次设定说她成天宅在家里玩电脑, ...

随机推荐

  1. 《Linux内核》读书笔记 第十八章

  2. 软件工程学习之小学四则混合运算出题软件 Version 1.1 设计思路及感想

    继上次采用形式文法来生成混合运算的算式,由于算法中没有引入控制参数而导致容易产生形式累赘(多余的括号等)的算式.本次更新决定采用一种更为简单有效的生成方式,由给出的一个随机的最终答案S,通过给定的一个 ...

  3. 软件工程学习之小学四则混合运算出题软件 Version 1.00 设计思路及感想

    对于小学四则混合运算出题软件的设计,通过分析设计要求,我觉得为了这个软件在今后便于功能上的扩充,可以利用上学期所学习的<编译原理>一课中的LL1语法分析及制导翻译的算法来实现.这样做的好处 ...

  4. jenkins配置过程中踩过的一些坑

    1,编译通过之后,想要将编译好的war包放到远程服务器上,并解压 unzipBus.sh的脚本如下: #!/bin/bash jar -xvf bus.war 编译后报错:jar:Command no ...

  5. Beta冲刺——day4

    Beta冲刺--day4 作业链接 Beta冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602134 王龙 ...

  6. shell 命令 if [ -d filename] 判断文件

    作者:曹毅涵  [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊 ...

  7. 2013长春网赛 1006 hdu 4764 Stone(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题意:Tang 和 Jiang 玩一个游戏,轮流写下一个数,Tang先手,第一次Tang只能写[ ...

  8. Java关于Robot类的使用

    利用Robot类实现自动操作,实现效果是运行之后鼠标自动定位到整个屏幕坐标系的(635,454)位置,输入wangtianze package com.wtz.util; import java.aw ...

  9. idea的激活

    参考网址:https://blog.csdn.net/qq_34273222/article/details/78810799 侵删 1.进到文件夹中:C:\Windows\System32\driv ...

  10. kubernetes1.8开启swagger-ui

    现在的版本默认只开启了6443安全端口,需要证书验证才能访问api,实现起来稍微有点麻烦,这里提供一个简单的方法. 先来看看官方说明: Complete API details are documen ...