heapsort.h

来自「这是数据结构、算法与应用-C++语言描述的代码」· C头文件 代码 · 共 29 行

H
29
字号
// heap sort a[1:n]
#ifndef heapSort_
#define heapSort_

#include "maxHeap.h"

using namespace std;

template <class T>
void heapSort(T a[], int n)
{// Sort a[1:n] using the heap sort method.
   // create a max heap of the elements
   maxHeap<T> heap(1);
   heap.initialize(a, n);

   // extract one by one from the max heap
   for (int i = n - 1; i >= 1; i--)
   {
      T x = heap.top();
      heap.pop();
      a[i+1] = x;
   }

   // save array a from heap destructor
   heap.deactivateArray();
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?