Heap-sort
来源:互联网 发布:阿里妈妈淘宝客赚钱 编辑:程序博客网 时间:2024/05/24 05:08
#include <stdio.h>#include <iostream>using namespace std;typedef int INDEX;typedef class Node { public: INDEX GetParentInd( INDEX nInd); INDEX GetLChilInd( INDEX nInd); INDEX GetRChilInd( INDEX nInd); }NODE;typedef class HeapSort{ public: HeapSort( int *array=NULL, INDEX right = 0); ~HeapSort( void); bool SetArray( int *array, INDEX right); bool sort( void); bool show( void); private: bool MaxHeapify( INDEX nInd, INDEX right); //swap value between two nodes. bool Swap( INDEX Ind1, INDEX Ind2); // get index of biggest node INDEX GetBigNodeInd( INDEX nInd, INDEX right); NODE fun; int *array; INDEX left; INDEX right;} HEAPSORT;INDEX Node::GetParentInd(INDEX nInd){//here is a trick to deal with the differentiation between left leaf and right leaf. return nInd /2;}INDEX Node::GetLChilInd(INDEX nInd){ return 2*(nInd + 1) -1;}INDEX Node::GetRChilInd(INDEX nInd){ return 2*(nInd + 1);}HeapSort::HeapSort(int * array , INDEX right ){ this->array = array; this->left = 0; this->right = right;}HeapSort::~HeapSort( void){}bool HeapSort::SetArray(int * array ,INDEX right){ this->array = array; this->left = 0; this->right = right; return true;}bool HeapSort::sort(void){ if( this->array==NULL) { cout<<"error: "<<__FILE__<<__LINE__<<endl; return false; }/** As we all know, some of nodes haven't child node.* For skip those nodes, we need to find the last parent node.** but How can we do that?** --the answer is the last child node.*/ INDEX nInd = 0; nInd = this->fun.GetParentInd( this->right );/** Adjust nodes from bottom to top.Function MaxHeapify() * will arrange this node and its's sublayer nodes to * a max binary tree. */ while( nInd>=0) { this->MaxHeapify( nInd, this->right); nInd--; }/** moving the largest one between all of nodes into a array,* and tidy this max binary tree. Repeat this process untill * we get all of nodes.*/ nInd = this->right; while( nInd>0 ) { this->Swap( 0, nInd); nInd --; this->MaxHeapify( 0, nInd); } return true;}bool HeapSort::MaxHeapify( INDEX nInd, INDEX right){ INDEX max = this->GetBigNodeInd( nInd, right); while( max!=nInd) { this->Swap( max, nInd); nInd = max; max = this->GetBigNodeInd( nInd, right); } return true;}bool HeapSort::Swap(INDEX Ind1,INDEX Ind2){ int tmp = this->array[Ind1]; this->array[Ind1] = this->array[Ind2]; this->array[Ind2] = tmp;return true;}//compare node and it's childs to find the biggest node between them.//if no child , return itself.INDEX HeapSort::GetBigNodeInd( INDEX nInd, INDEX right){ INDEX LChil = this->fun.GetLChilInd( nInd); INDEX RChil = this->fun.GetRChilInd( nInd); INDEX max = nInd; if( RChil <= right ) {//both two max = this->array[LChil]>this->array[RChil]? LChil:RChil; max = this->array[max]>this->array[nInd]?max:nInd; } else if( LChil <= right ) {//only one max = this->array[LChil]>this->array[nInd]?LChil:nInd; } return max;}bool HeapSort::show(void){ INDEX i; printf(" [left = %3d] ", this->left); for( i=this->left; i<=this->right; i++) { printf(" %4d", this->array[i]); } printf(" [right = %3d] \n", this->right); return true;}#define ARR_LEN15static intArr[ARR_LEN] = { 0,1,4,7,2,5,8,3,6,9,};int main( ){ HeapSort sort; sort.SetArray( Arr, ARR_LEN -1); sort.show( ); sort.sort( ); sort.show( ); return 0;}
0 0
- Heap Sort
- Heap Sort
- heap sort
- heap sort
- Heap Sort
- heap sort
- heap sort
- heap sort
- Heap-Sort
- Heap-sort
- Heap sort
- Heap Sort
- Heap Sort
- heap sort
- Heap Sort
- Heap Sort
- Heap Sort
- heap and heap sort
- nginx配置实现代理多个内网地址
- solr4.7.2中查询参数的意思
- 使用真机测试时,在eclipse中data文件夹打不开
- mysql 外键约束举例
- Java获取数据库各种查询结果
- Heap-sort
- 深入理解Magento – 第七章 – 自定义Magento系统配置
- leetcode: Best Time to Buy and Sell Stock III
- sublime下配置Python开发环境
- 编辑文章 - 博客频道 - CSDN.NET
- RandomAccessFile【2】
- 模式匹配KMP
- nand2tetris【2】
- startup_stm32f10x_xx.s 启动代码文件选择