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. VS2012常用快捷建(必备)

    1. 强迫智能感知:Ctrl+J:2.强迫智能感知显示参数信息:Ctrl-Shift-空格:3.格式化整个块:Ctrl+K+F4. 检查括号匹配(在左右括号间切换): Ctrl +]5. 选中从光标起 ...

  2. MYSQL while 、repeat

    前期准备: createt table employee(ID int ,Name varchar(4)); 用下列方法向表中循环插入数据. ----------------------------- ...

  3. VS调试时监视上一个错误代码和错误的文本描述

    以前我都是用GetLastError()然后在MSDN里面查错误原因的.现在才知道有很简便的方法: 在Watch窗口选择一行,然后输入$err,hr

  4. NFC通信的模式选择

    原帖请参照:http://www.nfcchina.org/forum.php?mod=viewthread&tid=68&extra=page%3D1 1.nfc 怎么选择操作模式的 ...

  5. 使用Flask 生成中文图片验证码

    因最近要用到验证码,上网搜了下,发现什么验证码感觉都能被攻破,连最近疯传的变态的12306的验证码居然有人一天就攻破了,所以,综合考虑,还是使用汉字: web框架是Flask,然后使用python的I ...

  6. apache的斜杠问题

    APACHE默认情况下,网页目录的最后必须加入斜杠“/",比如 可以浏览http://www.example.com/abc/,但是不能浏览http://www.example.com/ab ...

  7. UVA 10305 Ordering Tasks

    题意: 给出n和m,n代表总共有几个箱子.接下来m行,每行有a,b,表示b在a之后.输出一个合理的序列. 分析: 简单的拓扑排序: 代码: #include <iostream>#incl ...

  8. Jquery实现鼠标hover图片遮罩弹出提示层特效

    1.引入JS文件和所需要的样式文件 2.简单的写上以下18行代码就可以实现了 $(document).ready(function() { $("div.tip_trigger a.img& ...

  9. 使用 Oracle Sql plus的一点经验

    1    当你输入的语句有错误的时候,不用重新输入语句,直接输入ed就会出现一个文本文档显示之前输入的语句,这样你可以在文本文档里面修改语句,最后点保存. 2 三个set:设置每行显示的记录长度:SE ...

  10. android 后台运行

    改写返回键事件监听,使得back键功能类似home键,让Acitivty退至后台时不被系统销毁,代码如下: public boolean onKeyDown(int keyCode, KeyEvent ...