算法导论(build the heap recursively 递归建堆)
来源:互联网 发布:货到付款的淘宝有哪个 编辑:程序博客网 时间:2024/06/04 19:30
#include<iostream>#include<vector>#include<string>#include<set>#include<map>#include<unordered_set>#include<unordered_map>#include<algorithm>#include<xfunctional>using namespace std;typedef struct heap{int arr_size;int heap_size;int *arr;}heap;void Tune(heap& h,int start){int i = start;if (2 * i > h.heap_size) return;int index=-1;int max_temp = h.arr[i];if (h.arr[2 * i] > h.arr[i]){index = 2 * i;max_temp=h.arr[index];}if (2 * i + 1 <= h.heap_size){if (h.arr[2 * i + 1] > max_temp){max_temp = h.arr[2*i+1];index = 2 * i + 1;}}if (index == -1) return;else{swap(h.arr[index],h.arr[i]);Tune(h,index);}}void BuildHeap(heap &h){for (int i = h.heap_size / 2; i >0; i--){Tune(h,i);}}void heap_sort(heap &h){for (int i = h.heap_size; i > 1; i--){swap(h.arr[1], h.arr[h.heap_size]);h.heap_size--;Tune(h,1);}}int main(){heap h;cout << "Input the size of the array:";cin >> h.arr_size;h.arr = new int[h.arr_size+1];h.heap_size = h.arr_size;cout << "Input the element:";for (int i = 1; i <= h.arr_size; i++) cin >> h.arr[i];BuildHeap(h);cout << "After build:";for (int i = 1; i <= h.arr_size; i++) cout << h.arr[i] << " ";cout << endl;heap_sort(h);cout << "After sort:";for (int i = 1; i <= h.arr_size; i++) cout << h.arr[i]<<" ";cout << endl;system("pause");return 0;}
0 0
- 算法导论(build the heap recursively 递归建堆)
- 算法导论(build the heap iteratively 迭代建堆)
- 第六章堆排序之“建堆BUILD-MAX-HEAP”(递归版)
- 算法导论 习题2.3-4(the implemention of insertion sort recursively)
- 算法导论 Exercise 2.3-5 (implement binary serach recursively)
- Java插入排序递归算法(insert sorting recursively)
- 算法导论:堆排序递归实现 (c语言)
- 算法导论:堆排序非递归实现(c语言)
- 算法导论(Problems 6-2) d-ary heap(多叉树实现堆)
- 算法导论——堆排序(Heap-Sort)
- 堆(Heap)排序算法
- 第六章堆排序之“建堆BUILD-MAX-HEAP”(迭代版)
- 算法导论(Exercise 6.5-9 implement the k-way merge by using heap)
- 堆排序(算法导论)
- 堆排序(算法导论)
- 堆排序(最小堆)--【算法导论】
- 算法导论--堆排序(堆维护,建堆,堆排序)
- STL中heap算法(堆算法)
- awk详解
- NodeJS笔记
- HTML标签 英文全称 中文释义
- jquery.dataTable用法
- Codeforces Round #394 (Div. 2)
- 算法导论(build the heap recursively 递归建堆)
- 普世价值
- 配置maven报错
- 汇编翻转总结及答复(第8周)
- C# List Sort 排序
- ElasticSearch速学
- 团体程序设计天梯赛L2-024部落
- 自定义密码框 让你实现一格一格输密码
- StringBuider源码解析