堆 优先队列等
来源:互联网 发布:wordpress转zblog php 编辑:程序博客网 时间:2024/06/16 04:56
堆的简单实现,注意点和优化点还是比较多的,后面补充库的使用。
#include <iostream>using namespace std;void print(int *src, int len){ for(int i = 0; i < len; ++i) cout << src[i] << " "; cout << endl;}void prce_down(int *src, int parent, int len){ int tmp = src[parent]; for(int child=parent*2+1; child<len; child=2*child+1){ if(child+1<len && src[child]<src[child+1]){ ++child; } if(src[parent] < src[child]){ //swap(src[parent],src[child]); src[parent] = src[child]; parent = child; }else{ break; } } src[parent] = tmp;}void heap_sort(int *src, int len){ //先建立大顶堆 i 2*i+1 2*i+2 for(int i=len/2-1; i>=0; --i){ prce_down(src, i, len); } //print(src,len); //进行排序 for(int i=len-1; i>0; --i){ swap(src[0], src[i]); //将剩下的无序元素继续调整为大顶堆 prce_down(src,0,i); }}int main(){ int src[] = {33, 1, 3, 88, 6, 11, 2}; heap_sort(src, 7); for(int i = 0; i < sizeof(src)/sizeof(src[0]); ++i) cout << src[i] << " "; cout << endl; return 0;}
阅读全文
0 0
- 堆 优先队列等
- 堆和优先队列
- 堆和优先队列
- 优先队列--二叉堆
- 优先队列--堆
- 二叉堆/优先队列
- 优先队列(堆)
- 优先队列实现堆
- 堆 优先队列
- 优先队列(堆)
- 优先队列与堆
- 优先队列(堆)
- poj3253 堆/优先队列
- 优先队列 - 堆
- 优先队列(堆)
- 优先队列(堆)
- 堆,优先队列
- 堆和优先队列
- js驼峰命名和下划线转换
- Linux 知识点一
- PHP设计模式系列
- python3的switch case
- latex资源下载
- 堆 优先队列等
- html前端命名规则
- js基础笔记
- Hibernate搭建开发环境+简单实例(二)
- Recyclerview三种布局获取数据+Springview刷新
- Android中ViewGroup.Params整理总结
- 加油
- Github上怎么修改别人的项目并且提交给原作者!图文并茂!
- nginx