鱼C论坛

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

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

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

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

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

x
最大优先队列PQ.h
#if !defined (PQ_H)
#define PQ_H

template<class T>
class PriorityQueue{
private:
        T    *_pq;
        int  _N = 0;
public:
        PriorityQueue(int capacity);
        ~PriorityQueue();
        bool isEmpty() { return _N == 0; }
        void insert(T x);
        T    pop();
private:
        void swim(int k);
        void sink(int k);
        bool less(int i, int j) { return _pq[i] < _pq[j]; }
        void swap(int i, int j)
        {
                T ex   = _pq[i];
                _pq[i] = _pq[j];
                _pq[j] = ex;
        }
};

template<class T>
PriorityQueue<T>::PriorityQueue(int capacity)
{
        _pq = new T[capacity+1];
}

template<class T>
PriorityQueue<T>::~PriorityQueue(void)
{
        delete [] _pq;
}

template<class T>
void PriorityQueue<T>::swim(int k)
{
        while (k > 1 && less(k/2, k))
        {
                swap(k, k/2);
                k = k/2;
        }
}

template<class T>
void PriorityQueue<T>::insert(T x)
{
        _pq[++_N] = x;
        swim(_N);
}

template<class T>
void PriorityQueue<T>::sink(int k)
{
        while (2*k <= _N)
        {
                int j = 2*k;
                if (j < _N && less(j, j+1)) j++;
                if (!less(k, j)) break;
                swap(k, j);
                k = j;
        }
}

template<class T>
T PriorityQueue<T>::pop()
{
        T max = _pq[1];
        swap(1, _N--);
        sink(1);
        _pq[_N+1] = 0;
        return max;
}

#endif
堆排序(注意排序从a[1]开始,a[0]不参与。)

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

template<class T>
class Heap{
public:
        void sort(T a[], int n)
        {
                int N = n;
                for (int k = N/2; k >= 1; --k)
                        sink(a, k, N);
                while (N > 1)
                {
                        swap(a, 1, N);
                        sink(a, 1, --N);
                }
        }
private:
        void sink(T a[], int k, int N)
        {
                while (2*k <= N)
                {
                        int j = 2*k;
                        if (j < N && less(a, j, j+1)) j++;
                        if (!less(a, k, j)) break;
                        swap(a, k, j);
                        k = j;
                }
        }
        bool less(T a[], int v, int w)
        { return a[v] < a[w]; }
        void swap(T a[], int i, int j)
        {
                T ex = a[i];
                a[i] = a[j];
                a[j] = ex;
        }
};

#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-10-1 18:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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