CLRS:build_max_heap(strorage in array)
//用满二叉树存储,从n/2处开始递归向上调整(n/2后均为叶子节点,无需调整)使得根最大
//满二叉树顺序存储,左子2i,右子2i+1;
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000
#define RANDOM_SIZE 100
int buf [ARRAY_SIZE];
int n;
max_heap(int x)
{
int l=2*x,r=l+1,large=x;
if(l<=n&&buf[l]>buf[x])large=l;
if(r<=n&&buf[r]>buf[x])large=r;
if(large!=x)
{
int tmp=buf[large];buf[large]=buf[x];buf[x]=tmp;
max_heap(large);//recurrence adjust
}
}
int main()
{
srand((unsigned int )time(0));
int i;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)buf[i]=rand()%RANDOM_SIZE;
for(i=1;i<=n;i++)printf("%d ",buf[i]);
printf("*\n");
//creat random value of buffer and print
//build_heap
for(i=n/2;i>0;i--)
{
max_heap(i);
}
for(i=1;i<=n;i++)printf("%d ",buf[i]);
}
}
CLRS:build_max_heap(strorage in array)的更多相关文章
- 使用 JavaScript 实现名为 flatten(input) 的函数,可以将传入的 input 对象(Object 或者 Array)进行扁平化处理并返回结果
请使用 JavaScript 实现名为 flatten(input) 的函数,可以将传入的 input 对象(Object 或者 Array)进行扁平化处理并返回结果.具体效果如下: const in ...
- 引用类型(object、array)
1.Object类型 1)创建方法: //使用new加object构造函数 var person = new Object(); person.name = "aaa"; pers ...
- [翻译] C++ STL容器参考手册(第一章 <array>)
返回总册 本章节原文:http://www.cplusplus.com/reference/array/array/ 1. std::array (C++11支持) template < cla ...
- C#LeetCode刷题之#88-合并两个有序数组(Merge Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3686 访问. 给定两个有序整数数组 nums1 和 nums2, ...
- java 集合(Collection 和 Array)
Collection(是一个单列集合的根接口) Collections(操作集合对象的一个工具类)只要了解部分常用的方法就好
- LeetCode 88. 合并两个有序数组(Merge Sorted Array)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 色彩滤镜矩阵(Color Filter Array)
数码相机上的每个象素都带有一个光感应器,用以测量光线的明亮程度.由于光电二极管是只支持单颜色的装置,它不能区别不同波长的光线.因此,数码相机工程师在相机感应器的上部装上了一套镶嵌式的颜色滤镜,一个颜色 ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- 数组(Array),二维数组,三维数组
数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...
随机推荐
- C语言基础学习
汇编语言又叫符号语言 出来机器语言和汇编语言外其他语言必须经过翻译(编译,和解释行)才可以执行 .c --> 编译(翻译成二进制代码 .obj) 链接 把目标程序和库函数以及其他目标程序链接起来 ...
- C++ operator 知识点 2
http://blog.csdn.net/szlanny/article/details/4295854 operator它有两种用法,一种是operator overloading(操作符重载),一 ...
- WebBrowserControl
Before navigating the URL, write meta into webbrowser's documenttext property as follows: //Setting ...
- NLP学习资源
Journals ACM Transactions on Information Systems (TOIS) 影响因子 5.059(2006) IEEE Transactions on Knowl ...
- RPM软件包制作
参考网站:How to create an RPM package/zh-cn[教程]https://fedoraproject.org/wiki/How_to_create_an_RPM_packa ...
- C++学习16 继承时的名字遮蔽
如果派生类中的成员变量和基类中的成员变量重名,那么就会遮蔽从基类继承过来的成员变量.所谓遮蔽,就是使用新增的成员变量,而不使用继承来的. 成员函数也一样,如果函数名和参数签名都相同,就会造成遮蔽.如果 ...
- 为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调
我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在"生产者-消费者"模型中,生产者线程发现缓冲区满了就 ...
- (medium)LeetCode 230.Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- IOS学习之路- 运行过程
1. 执行Main函数(在main.m文件中) 2. 加载MainStoryborad.storyboard文件 * 创建ViewController文件 * 根据storyboard文件中描述创建V ...
- GL_INVALID_VALUE(0X501)
当android应该打开GPU的支持后, 有些手机会出现黑屏.闪屏等现象. 跟踪控制台, 会打印日志GL_INVALID_VALUE(0X501). 参考资料:http://mobile.riaos. ...