鱼C论坛

 找回密码
 立即注册
查看: 3144|回复: 7

[技术交流] 优先队列以及堆排序(C++)

[复制链接]
发表于 2014-2-12 11:00:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最大优先队列PQ.h

  1. #if !defined (PQ_H)
  2. #define PQ_H

  3. template<class T>
  4. class PriorityQueue{
  5. private:
  6.         T    *_pq;
  7.         int  _N = 0;
  8. public:
  9.         PriorityQueue(int capacity);
  10.         ~PriorityQueue();
  11.         bool isEmpty() { return _N == 0; }
  12.         void insert(T x);
  13.         T    pop();
  14. private:
  15.         void swim(int k);
  16.         void sink(int k);
  17.         bool less(int i, int j) { return _pq[i] < _pq[j]; }
  18.         void swap(int i, int j)
  19.         {
  20.                 T ex   = _pq[i];
  21.                 _pq[i] = _pq[j];
  22.                 _pq[j] = ex;
  23.         }
  24. };

  25. template<class T>
  26. PriorityQueue<T>::PriorityQueue(int capacity)
  27. {
  28.         _pq = new T[capacity+1];
  29. }

  30. template<class T>
  31. PriorityQueue<T>::~PriorityQueue(void)
  32. {
  33.         delete [] _pq;
  34. }

  35. template<class T>
  36. void PriorityQueue<T>::swim(int k)
  37. {
  38.         while (k > 1 && less(k/2, k))
  39.         {
  40.                 swap(k, k/2);
  41.                 k = k/2;
  42.         }
  43. }

  44. template<class T>
  45. void PriorityQueue<T>::insert(T x)
  46. {
  47.         _pq[++_N] = x;
  48.         swim(_N);
  49. }

  50. template<class T>
  51. void PriorityQueue<T>::sink(int k)
  52. {
  53.         while (2*k <= _N)
  54.         {
  55.                 int j = 2*k;
  56.                 if (j < _N && less(j, j+1)) j++;
  57.                 if (!less(k, j)) break;
  58.                 swap(k, j);
  59.                 k = j;
  60.         }
  61. }

  62. template<class T>
  63. T PriorityQueue<T>::pop()
  64. {
  65.         T max = _pq[1];
  66.         swap(1, _N--);
  67.         sink(1);
  68.         _pq[_N+1] = 0;
  69.         return max;
  70. }

  71. #endif
复制代码
堆排序(注意排序从a[1]开始,a[0]不参与。)

Heap.h
  1. #if !defined (HEAP_H)
  2. #define HEAP_H

  3. template<class T>
  4. class Heap{
  5. public:
  6.         void sort(T a[], int n)
  7.         {
  8.                 int N = n;
  9.                 for (int k = N/2; k >= 1; --k)
  10.                         sink(a, k, N);
  11.                 while (N > 1)
  12.                 {
  13.                         swap(a, 1, N);
  14.                         sink(a, 1, --N);
  15.                 }
  16.         }
  17. private:
  18.         void sink(T a[], int k, int N)
  19.         {
  20.                 while (2*k <= N)
  21.                 {
  22.                         int j = 2*k;
  23.                         if (j < N && less(a, j, j+1)) j++;
  24.                         if (!less(a, k, j)) break;
  25.                         swap(a, k, j);
  26.                         k = j;
  27.                 }
  28.         }
  29.         bool less(T a[], int v, int w)
  30.         { return a[v] < a[w]; }
  31.         void swap(T a[], int i, int j)
  32.         {
  33.                 T ex = a[i];
  34.                 a[i] = a[j];
  35.                 a[j] = ex;
  36.         }
  37. };

  38. #endif
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-28 11:57:19 | 显示全部楼层
太感谢了,楼主好人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-12 09:27:49 | 显示全部楼层
谢谢楼主分享!!!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-27 00:05:38 | 显示全部楼层
感谢楼主无私分享!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-28 09:24:48 | 显示全部楼层
支持楼主~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-31 10:24:08 | 显示全部楼层
支持小甲鱼!!支持小甲鱼!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-31 10:55:44 | 显示全部楼层
支持楼主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-31 12:34:49 | 显示全部楼层
强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表