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. SqlDataReader 获取存储过程返回值

    编写存储过程,获取不到返回值 附上代码: SqlDataReader reader = null;// totalRecords = ; try { SqlConnectionHolder conne ...

  2. U盘详解

    摘要:U盘,称呼最早来源于朗科公司生产的一种新型存储设备,名曰“优盘”,使用USB接口进行连接.USB接口就连到电脑的主机后,U盘的资料可与电脑交换.而之后生产的类似技术的设备由于朗科已进行专利注册, ...

  3. 浅谈38K红外发射接受编码

    之前做接触过一次红外遥控器,现在有空想用简单的话来聊一聊,下面有错误的地方欢迎改正指出:1:红外的概念不聊,那是一种物理存在.以下聊38K红外发射接收,主要讲可编程的红外编码.2:红外遥控 红外遥控首 ...

  4. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  5. hdu 4686 Arc of Dream_矩阵快速幂

    题意:略 构造出矩阵就行了 |   AX   0    AXBY   AXBY       0  |                                                   ...

  6. OpenStack IdentityService Keystone V3 API Curl实战

    v3 API Examples Using Curl <Tokens> 1,Default scope 获取token Get an token with default scope (m ...

  7. mysql root@::1 意义

    root@::1 ::1 是IPv6格式的 127.0.0.1

  8. 【每日一MOS】-RAC and Sequences (853652.1)

    序列有四种组合: a. CACHE + NOORDER b. CACHE + ORDER c. NOCACHE + NOORDER d. NOCACHE + ORDER 即使在单例配置下,当有大量的s ...

  9. 深入理解JVM : Java垃圾收集器

    如果说收集算法是内存回收的方法论,那么垃圾收集器就是内存回收的具体实现. Java虚拟机规范中对垃圾收集器应该如何实现并没有任何规定,因此不同的厂商.不同版本的虚拟机所提供的垃圾收集器都可能会有很大差 ...

  10. JavaScript 回车 焦点切换(摘抄)

    <!-- 这是回车转换行的代码段--> <script language='javascript' for='document' event='onkeydown'> if(e ...