趣解堆排序--老子儿子争王位
2020-07-14 05:45
204 查看
/* The MIT License (MIT)
Copyright (c) <2014> <oliver-lxtech2013@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "HeapSort.h" HeapSortArray::HeapSortArray() { //ctor } HeapSortArray::~HeapSortArray() { //dtor } /* * [public]建立最大堆 * @param a 堆化数组 */ void HeapSortArray::buildMaxHeap(vector<int> &a) { int len = a.size(); int i; //倒数第一个非叶子节点的下标为n/2 - 1(有孩子的节点为[len/2-1, 0]) for(i = len/2 - 1; i >= 0; i--) { maxHeapify(a, i, len); } } /* * [public]维护最大堆性质 * @param a 堆化数组 * @param index Root节点下标 * @param heap_size 调整的树长度 */ void HeapSortArray::maxHeapify(vector<int> &a, int root, int heap_size) { int l = 2 * root + 1; //左孩子,如果下标从0开始,Left(root) = 2 * root + 1; int r = l + 1; //右孩子 int largest = root; //首先老子认为老子自个的兵是最多的 /* 左边的孩子跟老子比兵权大小(记得先看看自个左边有没有孩子) */ if(l <= heap_size - 1 && a[l] > a[largest]) { largest = l; //我去,左边孩子的兵居然比我多,好吧,王位暂时让给你了. } /* 右孩子也要参与比多(记得看看右边有没有生孩子) */ if(r <= heap_size - 1 && a[r] > a[largest]) { largest = r; //我去,右边孩子又想和最多兵的人比比,好吧,右边孩子比较多,王位让给你. } /* * 如果老子我不是最大的,那没办法,最顶上的王位 * 要拱手让给最大的儿子 */ if(largest != root) { _swap(&a[root], &a[largest]); //这会,得和有最多兵的儿子换个位子 maxHeapify(a, largest, heap_size); //换到大儿子家,妈呀,还得看有没有打破儿子家的平衡,和孙子比比(交换后largest处大顶堆可能破坏,递归重新排) } } /* * [public]排序 */ void HeapSortArray::Sort() { /* 构建大顶堆 */ int heap_size = this->_sortArray.size(); //数据长度 buildMaxHeap(this->_sortArray); //建立大顶堆 int i; for(i = heap_size - 1; i > 0; i--) { _swap(&this->_sortArray[i], &this->_sortArray[0]); //交换,冒出最大Root maxHeapify(this->_sortArray, 0, i); //交换完后重新调整大顶堆 } } /* * [public]添加一个元素到末尾 * @param a 要添加的元素 * @retval none */ void HeapSortArray::Add(int a) { this->_sortArray.push_back(a); } /* * [private]获取某个数 * @param a 要交换数的a的地址 * @param b 要交换的数b的地址 */ int HeapSortArray::Get(int index) { return this->_sortArray[index]; } /* * [public]获取此序列里元素的个数 * @param a 要交换数的a的地址 * @param b 要交换的数b的地址 */ int HeapSortArray::Length() { return this->_sortArray.size(); } /* * [public]打印数组 */ void HeapSortArray::Printf() { int i = 0; cout<<"Array:"<<endl; for(; i < this->_sortArray.size(); i++) { cout<<"A"<<i<<":"<<this->_sortArray[i]<<endl; } } /* * [private]交换两个数 * @param a 要交换数的a的地址 * @param b 要交换的数b的地址 */ void HeapSortArray::_swap(int* a, int* b) { //如果两个地址相同,则不做交换 if(a == b) return; int mid = *a; *a = *b; *b = mid; }
转载于:https://www.cnblogs.com/openIoT/p/4108539.html
相关文章推荐
- Apriori-老子决定儿子的定律
- 扛不住了,老子就想独立上市的他,还是要当“儿子”了
- 儿子该如何找老子要钱------浅聊linux中的5种I/O模型并顺便说说异步和非阻塞的区别
- 他儿子就这样娶到了比尔·盖茨的女儿
- 从一个政客同儿子的谈话中得到的感想(一)
- 树的儿子链表表示法
- 两个 亿万富翁给儿子的信
- 儿子、房子和票子
- 儿子会走了
- 儿子34天黄疸未退(5.7日)
- 从老子那里获取智慧
- 为了公司的发展、员工有利益,老子每天吹牛!!!--咱们一起"骂"老板(3)
- 儿子叫良宸!
- 给儿子的新年寄语
- 看门人的儿子
- 给儿子的一封信
- 2008年7月14号,晴天,醒来的时候习惯的看了一下手机,发现太太在昨天晚上十一点四十五分中的时候给我发了一条信息,说她在床上想我和儿子,当时想给太太回复,但想想算了,太太看到我这么早给他发短信又要担心我没有睡好
- 儿子上幼儿园第三天
- 我儿子晓得害怕了,哈哈
- 儿子的两三事