码迷,mamicode.com
首页 > 编程语言 > 详细

算法导论——最大堆,以及堆排序算法

时间:2015-04-06 23:02:56      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

本段代码实现了建堆,维护最大堆的性质,堆排序函数,优先队列的相关函数(插入,找最大值,提取出最大值,增加关键值,增加元素),以及相关的测试 
1
#include <iostream> 2 #include <memory> 3 #include <iomanip> 4 #define LEFT(i) (2 * i) 5 #define RIGHT(i) (2*i + 1) 6 #define PARENT(i) (i >> 1) 7 8 using namespace std; 9 10 11 template< class T > 12 class myHeap 13 { 14 public: 15 int heapSize; 16 int maxSize; 17 T *data;//[heapSize];// = new T[heapSize]; 18 19 20 21 myHeap( T a[], int inputSize, int inputMaxSize ) 22 { 23 heapSize = inputSize; 24 maxSize = inputMaxSize; 25 data = new T[maxSize + 1]; 26 if( inputMaxSize < inputSize ) 27 { 28 cout << "error! input size is bigger than maxSize!" << endl; 29 exit( 0 ); 30 } 31 memcpy( &(data[1]), a, inputSize * sizeof( T ) ); 32 //debug 33 for ( int i = 1; i <= inputSize; i ++ ) 34 { 35 cout << setw(3) << data[i] << " "; 36 } 37 for ( int i = inputSize / 2; i >= 1; i -- ) 38 { 39 max_heapify( *this, i ); 40 } 41 cout << endl; 42 for ( int i = 1; i <= inputSize; i ++ ) 43 { 44 cout << setw(3) << data[i] << " "; 45 } 46 47 } 48 void max_heapify( myHeap< T > &A, int i ) 49 { 50 int l = LEFT(i); 51 int r = RIGHT(i); 52 int largest = i; 53 if( l <= A.heapSize && A.data[l] > A.data[i] ) 54 largest = l; 55 if( r <= A.heapSize && A.data[r] > A.data[largest] ) 56 largest = r; 57 if ( largest != i ) 58 { 59 T tem = A.data[i]; 60 A.data[i] = A.data[largest]; 61 A.data[largest] = tem; 62 max_heapify( A, largest ); 63 } 64 } 65 //void buildMaxHeap( T a[], int inputLength, int maxSize ); 66 void heap_insert( T x ); 67 T heap_maximum(); 68 T heap_extract_max(); 69 void heap_increase_key( int i, T key ); 70 void heap_output();// for debug 71 72 }; 73 template<class T> 74 T myHeap<T>::heap_maximum() 75 { 76 return data[1]; 77 } 78 template<class T> 79 T myHeap<T>::heap_extract_max() 80 { 81 if( heapSize < 1 ) 82 { 83 cout << "heap underflow!" << endl; 84 exit(1); 85 } 86 T max = data[1]; 87 data[1] = data[heapSize]; 88 heapSize --; 89 max_heapify( *this, 1 ); 90 } 91 template<class T> 92 void myHeap<T>::heap_increase_key( int i, T key ) 93 { 94 if ( key < data[i] ) 95 { 96 cerr << "new key is smaller than current key!\n"; 97 //exit(1); 98 return; 99 } 100 data[i] = key; 101 while ( i >1 && key > data[ PARENT(i) ] ) 102 { 103 data[i] = data[ PARENT(i) ]; 104 i = PARENT(i); 105 } 106 data[i] = key; 107 } 108 template<class T> 109 void myHeap<T>::heap_insert( T x ) 110 { 111 heapSize ++; 112 data[heapSize] = x - 3; 113 heap_increase_key( heapSize, x ); 114 } 115 116 template<class T> 117 void myHeap<T>::heap_output() 118 { 119 for ( int i = 1; i <= heapSize; i ++ ) 120 { 121 cout << setw(3) << data[i] << " "; 122 if( i % 20 == 0 ) 123 cout << endl; 124 } 125 cout << endl; 126 } 127 //template< typename T > 128 //void myHeap::max_heapify( myHeap< T > &A, int i ) 129 //{ 130 // int l = LEFT(i); 131 // int r = RIGHT(i); 132 // int largest = i; 133 // if( l <= A.heapSize && A.data[l] > A.data[i] ) 134 // largest = l; 135 // if( r <= A.heapSize && A.data[r] > A.data[largest] ) 136 // largest = r; 137 // if ( largest != i ) 138 // { 139 // T tem = A.data[i]; 140 // A.data[i] = A.data[largest]; 141 // A.data[largest] = tem; 142 // max_heapify( A, largest ); 143 // } 144 //} 145 146 template<typename T> 147 myHeap<T> buildMaxHeap( T a[], int inputSize, int inputMaxSize ) 148 { 149 myHeap<T> heapRet( a[], inputSize, inputMaxSize ); 150 return heapRet; 151 } 152 153 template< typename T > 154 void heapSort( T a[], int inputSize ) 155 { 156 myHeap<T> heapTem( a, inputSize, inputSize + 3 ); 157 for ( int i = heapTem.heapSize; i >= 2; i -- ) 158 { 159 T tem; 160 tem = heapTem.data[1]; 161 heapTem.data[1] = heapTem.data[heapTem.heapSize]; 162 heapTem.data[heapTem.heapSize] = tem; 163 heapTem.heapSize --; 164 heapTem.max_heapify( heapTem, 1 ); 165 } 166 cout << endl; 167 for ( int i = 1; i <= inputSize; i ++ ) 168 { 169 cout << setw(3)<< heapTem.data[i] << " "; 170 if( i%10 == 0 ) 171 cout << endl; 172 } 173 } 174 175 int main() 176 { 177 187 int a[] = { 1, 2, 4, 5, 66, 4, 78, 12, 14, 199, 278, 986,56 }; 188 myHeap<int> heap1( a, 13, 20 ); 189 cout << "\nheap1 Max is: " << heap1.heap_maximum() << endl; 190 heap1.heap_increase_key( 10, 25 ); 191 heap1.heap_output(); 192 heap1.heap_insert( 999 ); 193 heap1.heap_output(); 194 heap1.heap_extract_max(); 195 heap1.heap_output(); 196 197 heapSort( a, 13 ); 198 199 return 0; 200 201 }

 

算法导论——最大堆,以及堆排序算法

标签:

原文地址:http://www.cnblogs.com/wangxuya/p/4396945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!