** preface **
本文是自己写的堆排序做下记录,不带讲解,没什么参考价值。文末有我看的一些博客的链接。
** code(C++) **
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
   | #include <iostream>
  using namespace std; int arrs[] = { 23, 65, 12, 3, 8, 76, 345, 90, 21, 75, 34, 61 }; int arrLen = sizeof(arrs) / sizeof(arrs[0]);
  void adjustHeap(int * arrs, int p, int len){     int curParent = arrs[p];     int child = 2* p + 1;        while(child < len){              if(child+1<len&&arrs[child]<arrs[child+1]){             child++;             }         if(curParent<arrs[child]){             arrs[p]=arrs[child];                                       p=child;             child=2*p+1;         }         else             break;     }     arrs[p]=curParent; }
  void heapSort(int * arrs, int len){          for(int i = arrLen /2 -1; i>=0; i--)         adjustHeap(arrs, i, arrLen);     for(int i = arrLen -1; i>=0; i--){         int maxEle = arrs[0];         arrs[0] = arrs[i];         arrs[i] = maxEle;
          adjustHeap(arrs, 0, i);     } }
  int main() {     heapSort(arrs, arrLen );     for (int i = 0; i < arrLen; i++)         cout << arrs[i] << endl;     return 0; }
 
   | 
 
** 参考 **
图解堆排序
堆排序C++实现
算法入门:堆排序