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] ; ...
随机推荐
- Excel 操作类
转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...
- (C/C++) Interview in English - Class
Q: What is a class? A: A class is an expanded concept of a data structure: instead of holding only d ...
- VS2013 越来越慢
Q.VS2013 原来启动只要大概 一两秒的时间,现在启动最少也得十秒以上.而且打开项目也变得很慢了!求解决方案. 清理一下缓存就好了.devenv.exe /resetuserdata 第二:装了v ...
- DBA_Oracle DBA常用表汇总(概念)
2014-06-20 Created By BaoXinjian
- dede图片横向滚动
<div id=demo style="overflow:hidden; width:960px;" > <table border=0 align=" ...
- python实现字体闪图
!/usr/bin/env python from future import print_function import os.path import sys from optparse impor ...
- 转-Activity中使用orientation属性讲解及需注意的问题
http://www.software8.co/wzjs/yidongkaifa/6504.html 今天遇到了一个关于orientation的问题查了点资料记录一下,只有点点滴滴的积累,才能让我们更 ...
- JAVA 匿名对象
/* 匿名对象: 没有名字的对象 匿名对象的使用方式之一: 当对对象方法只调用一次时,我们可以用匿名对象来完成,比较简化. 匿名对象的使用方式之二: 匿名对象可以被当做实参传递 */ class Ca ...
- 如何为PHP贡献代码
PHP在之前把源代码迁移到了git下管理, 同时也在github(https://github.com/php/php-src)上做了镜像, 这样一来, 就方便了更多的开发者为PHP来贡献代码. 今天 ...
- Informix SDK對比
一.基本信息對比 表 1. Informix .NET Provider 和 IBM Data Server .NET Provider 的对比 特性 IBM Informix .NET Provid ...