堆排序模板
来源:互联网 发布:unity3d 麻将开发 编辑:程序博客网 时间:2024/05/29 07:35
#pragma warning(disable:4996)#include <iostream>#include <vector>#include <algorithm>#include <queue>#include <string>#include <stdio.h>#include <limits.h>using namespace std;void heap_adjust(vector<int>& nums, int root, int hs){int left = root << 1;int right = (root << 1) + 1;int maxx = root;if (left > hs) return;if (nums[left] > nums[maxx]) maxx = left;if (right <= hs && nums[right] > nums[maxx]) maxx = right;if (maxx != root){swap(nums[maxx], nums[root]);heap_adjust(nums, maxx, hs);}}void build_heap(vector<int>& nums){for (int i = (nums.size()-1) >> 1; i >= 1; i--){heap_adjust(nums, i, nums.size()-1);}}int main(){//the index of heap begins at 1 and index 0 is a placeholdervector<int> heap{0,9,8,7,6,5,5,4,1,2,3,0,5,6,8,7,4,1,5,6};build_heap(heap);for (int i = heap.size() - 1; i >= 2; i--){swap(heap[1], heap[i]);heap_adjust(heap, 1, i-1);}return 0;}
0 0
- 【ACM模板】堆排序
- 堆排序模板
- C++堆排序模板
- 堆排序【模板】
- 堆排序模板
- 堆排序模板
- C++模板堆排序
- 【模板】堆排序
- 堆排序的模板
- 堆排序模板
- 堆排序【模板】
- 堆排序,C++模板编程
- 堆排序(Heap_Sort)模板
- 堆排序,C++模板编程
- C++模板之堆排序
- C++模板实现堆排序
- 算法--堆排序学习以及模板
- 堆排序(可以作为模板)
- 故地重游
- JS中BOM部分基础知识总结
- 使用redis的setbit和bitcount来进行区间统计的坑
- 使用Material Design中 elevation来设置阴影失效
- java本地缓存实现
- 堆排序模板
- java 多个第三方jar包冲突解决方案
- NOIP2011 铺地毯 选择客栈 Mayan游戏
- HDU 1466 计算直线的交点数
- poj 1837 Balance (背包)
- 认识HTML5
- [LeetCode]54. Spiral Matrix
- poj 3349 Snowflake Snow Snowflakes (哈希)@
- Matlab/C++读写系统时间