Heap sorting and heap sorting algorithms
<Pre name = "code" class = "cpp"> <pre name = "code" class = "cpp">/** heap sorting * // ** sequential table Storage * /# include <cstdio> # include <algorithm> # define LT (, b) (a) <(B) using namespace std; int a [] = {9, 5, 2, 1, 3, 4, 6, 8, 9, 10}; int n = 5; void f (int s, int m) {/** to make a [s... m] into a large top heap */int c = a [s]; for (int j = 2 * s; j <= m; j * = 2) {/** filter down child nodes with large keys */if (j <m & LT (a [j], a [j + 1]) + + j;/** j is the subscript of a large key record */if (! LT (c, a [j]) break;/** rc should be inserted on location s */a [s] = a [j]; s = j ;} a [s] = c;} void Hs () {/** put a [1... n] build a big top heap */for (int I = n/2; I> 0; -- I) f (I, n); for (int I = n; i> 1; -- I) {swap (a [1], a [I]);/** swap the largest heap top with the last one */f (1, i-1);/** re-tune a [1, I-1] to the Big Top heap */} int main () {Hs (); for (int I = 1; I <= 5; I ++) printf ("% d", a [I]); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.