There two methods to construct a heap from a unordered set of array.

If a array has size n, it can be seen as a complete binary tree, in which the element indexed by i has its left children 2*i+1(if 2*i+1<n) and its right children 2*i+2(if 2*i+2<n), noting that the index of the array is from 0 to n-1.
First let us introduce two subprocessed:
sift_down and
sift-up

sift-down

sift-down is a recursive procedure. Aussming that we start from node i, compare i with the smaller(denoted by j) between it's left children i+1 and it's right children i+2. If value of i is bigger than value of j(in min heap), we change the value of i and j, and then do the same procidure to j. Do like this until j is a leaf node. Note that the subtree rooted by left children of i and the subtree rooted by the right children of i are both minheap(satisfy the property of min heap). The code for sift-down can be written as follows:
void siftdown(int a[],int i, int n) //n is the size of array a
{
while(2*i+1<n)
{
int j=2*i+1;
if(j+1<n&&a[j+1]<a[j])
j++;
if(a[j]<a[i])
swap(a,i,j); //exchange value of i and j
i=j;
}
}

sift-up

sift-up is also a recursive procidure. Assuming that we start from node i, compare i with its parent p((i-1)/2). If value of i is smaller than value of p, exchange value of i and p, and then do the same thing to p until p is the root of this tree. Note that all the nodes before node i make up a minheap.  The code for sift-up can be written like follows:
void siftup(int a[],int i, int n) //n is the size of array a
{
while(i>0)
{
int p=(i-1)>>1;
if(a[i]<a[p])
swap(a,i,p);
i=p;
}
}

1、process using sift-down

The last element who has a children is indexed by (n-1)/2. Starting from i=(n-1)/2, Do sift-down to i until the root. After this, a minheap is constructed. The pseudo code for this procedure can be written like follows:
void heap_create_1(int a[],int n)
{
if(n<=1)
return;
int i=(n-1)/2;
while(i>0)
siftdown(a,i,n);
}

The time cost using only sift-down to create a heap is O(n).(Actrually, the compare times during creating a minheap from a unordered array, whose size is n, is not greater than 4*n.)

Note that in this method, when siftdown node i, all the subtree under i is minheap.

2、process using sift-up

This method go through from node indexed by 0 to node indexed by n-1. When processing node i, the nodes before i make up a minheap. So processing node i can be seen as inserting a new node to a minheap. For each i, we sift up from i to root. The pseudo code for this method can be written like follows:
void heap_create_2(int a[],int n)
{
int i;
for(i=1;i<n;i++)
siftup(a,i,n);
}

The time cost using sift-up to create a heap is O(nlogn).


heap creation的更多相关文章

  1. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  2. Hulu面试题解答——N位数去除K个数字(解法错误sorry)

    给定一个N位数,比如12345,从里面去掉k个数字.得到一个N-k位的数.比如去掉2,4,得到135,去掉1,5.得到234.设计算法.求出全部得到的N-k位数里面最小的那一个. 写的代码例如以下,思 ...

  3. [20190415]11g下那些latch是共享的.txt

    [20190415]11g下那些latch是共享的.txt http://andreynikolaev.wordpress.com/2010/11/23/shared-latches-by-oracl ...

  4. Linux Process/Thread Creation、Linux Process Principle、sys_fork、sys_execve、glibc fork/execve api sourcecode

    相关学习资料 linux内核设计与实现+原书第3版.pdf(.3章) 深入linux内核架构(中文版).pdf 深入理解linux内核中文第三版.pdf <独辟蹊径品内核Linux内核源代码导读 ...

  5. Heap Only Tuples (HOT)

    Introduction ------------ The Heap Only Tuple (HOT) feature eliminates redundant index entries and a ...

  6. [No0000147]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈4/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  7. mysql性能问题小解 Converting HEAP to MyIsam create_myisa

    安定北京被性能测试困扰了N天,实在没想法去解决了,今天又收到上级的命令说安定北京要解决,无奈!把项目组唯一的DBA辞掉了,现在所以数据库的问题都得自己来处理:( 不知道上边人怎么想的.而且更不知道怎安 ...

  8. java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出

    上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...

  9. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

随机推荐

  1. ZigBee技术简介

    Zigbee的由来 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂,功耗大,距离近,组网规模太小等,……而工业自 ...

  2. GIS 相关知识扫盲

    1.什么是GIS GIS:地理信息系统,它是一种特定的十分重要的空间信息系统.它是在计算机硬.软件系统支持下,对整个或部分地球表层(包括大气层)空间中的有关地理分布数据进行采集.储存.管理.运算.分析 ...

  3. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  4. Ubuntu 14.04 下使用IDEA开发Spark应用 入门

    网上有很多教程,有用sbt ,也有不用sbt的,看的头大,搞了半天,终于运行成功一个例子,如下: 1.官网下载http://www.jetbrains.com/idea/download/ Inter ...

  5. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  6. [笔记] /etc/init.d/ 下脚本的通用结构

    http://sunxiaqw.blog.163.com/blog/static/99065438201111715813443/ 下面以 named 为例 : #!/bin/bash # # nam ...

  7. cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第七步---英雄要升级&amp;属性--解析csv配置文件

    /* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦.他说:随便写,第一别全然照搬代码:第二能够说 ...

  8. Android应用开发基本流程及测试运行

    (1)Android App工程项目的创建 File—New—New Project 设置项目.程序包的名称 设置项目名称为MyDiary,程序包名为org.socrates.mydiary.acti ...

  9. 解决UITableView数据没有充满屏幕时,显示多余的空白cell的问题

    #pragma mark 去除多余的线 -(void) clearExtrLine{ UIView *view = [[UIView alloc] init]; view.backgroundColo ...

  10. hibernate异常

    <h1> nested exception is org.hibernate.LazyInitializationException:</h1> stackoverflow:h ...