堆和堆排序
三路快速排序
基本规则: 从数组1号位置开始,每个元素的leftChild为2k,rightChild为2k+1。 从数组0号位置开始,每个元素的leftChild为2k+1,rightChild为2k+2。
1 二叉树存取数据的实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 template <typename Item>class MaxHeap {private : Item *data; int count; int capacity; void shiftUp (int k) { while ( k > 1 && data[k/2 ] < data[k] ){ swap( data[k/2 ], data[k] ); k /= 2 ; } } void shiftDown (int k) { while ( 2 *k <= count ){ int j = 2 *k; if ( j+1 <= count && data[j+1 ] > data[j] ) j ++; if ( data[k] >= data[j] ) break ; swap( data[k] , data[j] ); k = j; } } public : MaxHeap(int capacity){ data = new Item[capacity+1 ]; count = 0 ; this ->capacity = capacity; } MaxHeap(Item arr[], int n){ data = new Item[n+1 ]; capacity = n; for ( int i = 0 ; i < n ; i ++ ) data[i+1 ] = arr[i]; count = n; for ( int i = count/2 ; i >= 1 ; i -- ) shiftDown(i); } ~MaxHeap(){ delete [] data; } int size () { return count; } bool isEmpty () { return count == 0 ; } void insert (Item item) { assert( count + 1 <= capacity ); data[count+1 ] = item; shiftUp(count+1 ); count ++; } Item extractMax () { assert( count > 0 ); Item ret = data[1 ]; swap( data[1 ] , data[count] ); count --; shiftDown(1 ); return ret; } Item getMax () { assert( count > 0 ); return data[1 ]; } };
2 基础堆排序 基础堆排序在静态数据中的效率慢于常规排序算法,适用于动态数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 template <typename T>void heapSort1 (T arr[], int n) { MaxHeap<T> maxheap = MaxHeap<T>(n); for ( int i = 0 ; i < n ; i ++ ) maxheap.insert(arr[i]); for ( int i = n-1 ; i >= 0 ; i-- ) arr[i] = maxheap.extractMax(); } template <typename T>void heapSort2 (T arr[], int n) { MaxHeap<T> maxheap = MaxHeap<T>(arr,n); for ( int i = n-1 ; i >= 0 ; i-- ) arr[i] = maxheap.extractMax(); }
3 优化堆排序 原地堆排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 template <typename T>void __shiftDown(T arr[], int n, int k){ while ( 2 *k+1 < n ){ int j = 2 *k+1 ; if ( j+1 < n && arr[j+1 ] > arr[j] ) j += 1 ; if ( arr[k] >= arr[j] )break ; swap( arr[k] , arr[j] ); k = j; } } template <typename T>void __shiftDown2(T arr[], int n, int k){ T e = arr[k]; while ( 2 *k+1 < n ){ int j = 2 *k+1 ; if ( j+1 < n && arr[j+1 ] > arr[j] ) j += 1 ; if ( e >= arr[j] ) break ; arr[k] = arr[j]; k = j; } arr[k] = e; } template <typename T>void heapSort (T arr[], int n) { for ( int i = (n-1 -1 )/2 ; i >= 0 ; i -- ) __shiftDown2(arr, n, i); for ( int i = n-1 ; i > 0 ; i-- ){ swap( arr[0 ] , arr[i] ); __shiftDown2(arr, i, 0 ); } }
4 索引堆
索引堆
对索引进行操作,而不是对值进行操作,可以在打乱顺序后找到数据对应的索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 #include <iostream> #include <cassert> #include "SortTestHelper.h" using namespace std ;template <typename Item>class IndexMaxHeap {private : Item *data; int *indexes; int count; int capacity; void shiftUp ( int k ) { while ( k > 1 && data[indexes[k/2 ]] < data[indexes[k]] ){ swap( indexes[k/2 ] , indexes[k] ); k /= 2 ; } } void shiftDown ( int k ) { while ( 2 *k <= count ){ int j = 2 *k; if ( j + 1 <= count && data[indexes[j+1 ]] > data[indexes[j]] ) j += 1 ; if ( data[indexes[k]] >= data[indexes[j]] ) break ; swap( indexes[k] , indexes[j] ); k = j; } } public : IndexMaxHeap(int capacity){ data = new Item[capacity+1 ]; indexes = new int [capacity+1 ]; count = 0 ; this ->capacity = capacity; } ~IndexMaxHeap(){ delete [] data; delete [] indexes; } int size () { return count; } bool isEmpty () { return count == 0 ; } void insert (int i, Item item) { assert( count + 1 <= capacity ); assert( i + 1 >= 1 && i + 1 <= capacity ); i += 1 ; data[i] = item; indexes[count+1 ] = i; count++; shiftUp(count); } Item extractMax () { assert( count > 0 ); Item ret = data[indexes[1 ]]; swap( indexes[1 ] , indexes[count] ); count--; shiftDown(1 ); return ret; } int extractMaxIndex () { assert( count > 0 ); int ret = indexes[1 ] - 1 ; swap( indexes[1 ] , indexes[count] ); count--; shiftDown(1 ); return ret; } Item getMax () { assert( count > 0 ); return data[indexes[1 ]]; } int getMaxIndex () { assert( count > 0 ); return indexes[1 ]-1 ; } Item getItem ( int i ) { assert( i + 1 >= 1 && i + 1 <= capacity ); return data[i+1 ]; } void change ( int i , Item newItem ) { i += 1 ; data[i] = newItem; for ( int j = 1 ; j <= count ; j ++ ) if ( indexes[j] == i ){ shiftUp(j); shiftDown(j); return ; } } bool testIndexes () { int *copyIndexes = new int [count+1 ]; for ( int i = 0 ; i <= count ; i ++ ) copyIndexes[i] = indexes[i]; copyIndexes[0 ] = 0 ; std ::sort(copyIndexes, copyIndexes + count + 1 ); bool res = true ; for ( int i = 1 ; i <= count ; i ++ ) if ( copyIndexes[i-1 ] + 1 != copyIndexes[i] ){ res = false ; break ; } delete [] copyIndexes; if ( !res ){ cout <<"Error!" <<endl ; return false ; } return true ; } }; template <typename T>void heapSortUsingIndexMaxHeap (T arr[], int n) { IndexMaxHeap<T> indexMaxHeap = IndexMaxHeap<T>(n); for ( int i = 0 ; i < n ; i ++ ) indexMaxHeap.insert( i , arr[i] ); assert( indexMaxHeap.testIndexes() ); for ( int i = n-1 ; i >= 0 ; i -- ) arr[i] = indexMaxHeap.extractMax(); } int main () { int n = 1000000 ; int * arr = SortTestHelper::generateRandomArray(n, 0 , n); SortTestHelper::testSort("Heap Sort Using Index-Max-Heap" , heapSortUsingIndexMaxHeap, arr, n); delete [] arr; return 0 ; }
5 优化索引堆
优化索引堆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 #include <iostream> #include <cassert> #include "SortTestHelper.h" using namespace std ;template <typename Item>class IndexMaxHeap {private : Item *data; int *indexes; int *reverse; int count; int capacity; void shiftUp ( int k ) { while ( k > 1 && data[indexes[k/2 ]] < data[indexes[k]] ){ swap( indexes[k/2 ] , indexes[k] ); reverse[indexes[k/2 ]] = k/2 ; reverse[indexes[k]] = k; k /= 2 ; } } void shiftDown ( int k ) { while ( 2 *k <= count ){ int j = 2 *k; if ( j + 1 <= count && data[indexes[j+1 ]] > data[indexes[j]] ) j += 1 ; if ( data[indexes[k]] >= data[indexes[j]] ) break ; swap( indexes[k] , indexes[j] ); reverse[indexes[k]] = k; reverse[indexes[j]] = j; k = j; } } public : IndexMaxHeap(int capacity){ data = new Item[capacity+1 ]; indexes = new int [capacity+1 ]; reverse = new int [capacity+1 ]; for ( int i = 0 ; i <= capacity ; i ++ ) reverse[i] = 0 ; count = 0 ; this ->capacity = capacity; } ~IndexMaxHeap(){ delete [] data; delete [] indexes; delete [] reverse; } int size () { return count; } bool isEmpty () { return count == 0 ; } void insert (int i, Item item) { assert( count + 1 <= capacity ); assert( i + 1 >= 1 && i + 1 <= capacity ); assert( !contain(i) ); i += 1 ; data[i] = item; indexes[count+1 ] = i; reverse[i] = count+1 ; count++; shiftUp(count); } Item extractMax () { assert( count > 0 ); Item ret = data[indexes[1 ]]; swap( indexes[1 ] , indexes[count] ); reverse[indexes[count]] = 0 ; count--; if (count){ reverse[indexes[1 ]] = 1 ; shiftDown(1 ); } return ret; } int extractMaxIndex () { assert( count > 0 ); int ret = indexes[1 ] - 1 ; swap( indexes[1 ] , indexes[count] ); reverse[indexes[count]] = 0 ; count--; if (count) { reverse[indexes[1 ]] = 1 ; shiftDown(1 ); } return ret; } Item getMax () { assert( count > 0 ); return data[indexes[1 ]]; } int getMaxIndex () { assert( count > 0 ); return indexes[1 ]-1 ; } bool contain ( int i ) { assert( i + 1 >= 1 && i + 1 <= capacity ); return reverse[i+1 ] != 0 ; } Item getItem ( int i ) { assert( contain(i) ); return data[i+1 ]; } void change ( int i , Item newItem ) { assert( contain(i) ); i += 1 ; data[i] = newItem; shiftUp( reverse[i] ); shiftDown( reverse[i] ); } bool testIndexesAndReverseIndexes () { int *copyIndexes = new int [count+1 ]; int *copyReverseIndexes = new int [count+1 ]; for ( int i = 0 ; i <= count ; i ++ ){ copyIndexes[i] = indexes[i]; copyReverseIndexes[i] = reverse[i]; } copyIndexes[0 ] = copyReverseIndexes[0 ] = 0 ; std ::sort(copyIndexes, copyIndexes + count + 1 ); std ::sort(copyReverseIndexes, copyReverseIndexes + count + 1 ); bool res = true ; for ( int i = 1 ; i <= count ; i ++ ) if ( copyIndexes[i-1 ] + 1 != copyIndexes[i] || copyReverseIndexes[i-1 ] + 1 != copyReverseIndexes[i] ){ res = false ; break ; } delete [] copyIndexes; delete [] copyReverseIndexes; if ( !res ){ cout <<"Error!" <<endl ; return false ; } for ( int i = 1 ; i <= count ; i ++ ) if ( reverse[ indexes[i] ] != i ){ cout <<"Error 2" <<endl ; return false ; } return true ; } }; template <typename T>void heapSortUsingIndexMaxHeap (T arr[], int n) { IndexMaxHeap<T> indexMaxHeap = IndexMaxHeap<T>(n); for ( int i = 0 ; i < n ; i ++ ) indexMaxHeap.insert( i , arr[i] ); assert( indexMaxHeap.testIndexesAndReverseIndexes() ); for ( int i = n-1 ; i >= 0 ; i -- ) arr[i] = indexMaxHeap.extractMax(); } int main () { int n = 1000000 ; int * arr = SortTestHelper::generateRandomArray(n, 0 , n); SortTestHelper::testSort("Heap Sort Using Index-Max-Heap" , heapSortUsingIndexMaxHeap, arr, n); delete [] arr; return 0 ; }
最后更新时间:2020-05-07 15:14:55
转载请注明出处