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. 为什么java的main方法必须是静态的

    今天看类型信息时发现一个问题,不能再main方法中打印this关键字的信息,这时想起了之前的知识,不能再静态方法中调用this.理由很简单,this表示“这个对象”,也就是声明一个类的对象,然而静态方 ...

  2. Linux上部署Tomcat+Nginx (JavaWeb项目)

    https://blog.csdn.net/wohiusdashi/article/details/81147059

  3. 解决局域网IP冲突

    进入cmd ipconfig -all 查看现有IP,发现IP不是192.168.1.*的形式,而是192.168.0.*等异常 ipconfig -release  释放现有IP ipconfig ...

  4. 【转帖】Git学习笔记 记录一下

    本文内容参考了廖雪峰老师的博文,并做了适当整理,方便大家查阅. 原帖地址 https://wangfanggang.com/Git/git/ 常用命令 仓库初始化 - git init 1 git i ...

  5. SpringBoot 2.SpringBoot整合Mybatis

    一.创建Springboot的配置文件:application.properties,并添加MyBatis依赖 SpringApplication 会从 application.properties  ...

  6. emoji & click copy

    emoji & click copy document.execCommand("copy"); https://clipboardjs.com/ https://www. ...

  7. List元素删除不会导致越界但有问题的写法

    今天在论坛里看到一段请教list删除的问题,下面先看代码: public static void main(String[] args) { List<Integer> list = ne ...

  8. spring 事务传播 never 当一个业务方法设置为never时候表示 不会加入任何事务中

  9. 【loj6029】「雅礼集训 2017 Day1」市场 线段树+均摊分析

    题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有四种:区间加.区间下取整除.区间求最小值.区间求和. $n\le 100000$ ,每次加的数在 $[-10^4,10^4]$ 之 ...

  10. 如何把EntityList转换成DataSet

    public static DataSet ToDataSet<TSource>(this IList<TSource> list) { Type elementType = ...