调整堆的程序
来源:互联网 发布:国元 8月宏观数据点评 编辑:程序博客网 时间:2024/06/16 07:59
void HeapAdjust(int a[], int root, int j); //root是根的下标,j是堆最后一个元素的数组下标,数组存储完全二叉树
void HeapAdjust_improve(int a[], int root, int j); //root是根的下标,j是堆最后一个元素的数组下标,数组存储完全二叉树
void main()
{
int a[8] = {65, 76, 13, 49, 49, 97, 27, 38};
for( int i = 3; i >= 0; --i ){
HeapAdjust_improve(a, i, 7);
for( auto f : a )
cout << f << " ";
cout << endl;
}
}
void HeapAdjust(int a[], int root, int j) //root是根的下标,j是堆最后一个元素的数组下标,数组存储完全二叉树,最小堆
{
int rc = a[root]; //parent = root, leftchild = 2 * root + 1, rightchild = 2 * root +2
for( int i = 2 * root + 1; i <= j; i = 2 * i + 1 )
{
if( i < j && a[i] > a[i+1] ) ++i;
if( a[root] > a[i] )
{
a[root] ^= a[i];
a[i] ^= a[root];
a[root] ^= a[i];
root = i;
}
else
break;
}
}
void HeapAdjust_improve(int a[], int root, int j)
{
int rc = a[root]; //parent = root, leftchild = 2 * root + 1, rightchild = 2 * root +2
for( int i = 2 * root + 1; i <= j; i = 2 * i + 1 )
{
if( i < j && a[i] > a[i+1] ) ++i;
if( rc > a[i] )
{
a[root] = a[i];
root = i;
}
else
break;
}
a[root] = rc;
}
- 调整堆的程序
- 最大堆的调整
- 堆的调整
- JVM堆大小的调整
- zoj2724 堆的插入调整
- JVM堆大小的调整
- 最大堆的插入/删除/调整/排序操作(图解+程序)(JAVA)
- 最大堆的插入/删除/调整/排序操作(图解+程序)(JAVA)
- 最小堆的上调整和下调整
- Sun HotSpot JVM堆大小的调整
- 最小堆的调整、插入和删除
- 堆排序--小根堆的建立与调整
- PAT L2-012. 关于堆的判断(最小堆调整)
- LintCode 130 [递归]堆化---堆的调整
- 浅谈堆排序的堆调整及时间复杂度
- 堆排序与堆向上调整与向下调整的C++实现
- 调整ABAP程序的性能
- 监听程序性能的调整
- HTTP协议post请求返回json数据
- mac 环境下 LuaJit 执行报错 : unknown luaJIT command or jit.* modules not installed
- Struts2第六篇【文件上传和下载】
- Mysql优化
- Problem E: 平面上的点和线——Point类、Line类 (V)
- 调整堆的程序
- 详解web.xml 中的listener、 filter、servlet 加载顺序
- javaweb之旅感慨
- Angular快速入门--路由篇
- cv2.imread()和caffe.io.loadimage的区别
- 谈谈“搜索”,2种场景下的最优解
- 友元函数、友元类、访问私有成员
- 关于定义静态Fragment的一个大坑!!!
- Problem F: 平面上的点和线——Point类、Line类 (VI)