1.binary heap实现

BinaryHeap.h

#ifndef BINARYHEAP_H
#define BINARYHEAP_H class BinaryHeap
{
public:
BinaryHeap(int N);
bool isEmpty();
void exchange(int i,int j);
void insert(int key);
int delMax();
int getMax();
virtual ~BinaryHeap();
protected:
private:
int N;
int * data;
void swim(int key);
void sink(int key);
}; #endif // BINARYHEAP_H

BinaryHeap.cpp

#include "BinaryHeap.h"
#include <stdlib.h> BinaryHeap::BinaryHeap(int N)
{
this->N = 0;
this->data = (int *)malloc(sizeof(int)*(N+1));
} BinaryHeap::~BinaryHeap()
{
} bool BinaryHeap::isEmpty()
{
return N == 0;
} void BinaryHeap::exchange(int i,int j)
{
int temp = this->data[i];
this->data[i] = this->data[j];
this->data[j] = temp;
} void BinaryHeap::swim(int key)
{
while(key > 1 && data[key/2] < data[key])
{
exchange(key/2,key);
key = key/2;
}
} void BinaryHeap::insert(int key)
{
N++;
data[N] = key;
swim(N);
} void BinaryHeap::sink(int key)
{
while(key*2 <= N)
{
int j = key*2;
if(j < N && data[j] < data[j+1])
j++;
if(data[key] >= data[j])
break;
else
{
exchange(key,j);
key = j;
}
}
} int BinaryHeap::delMax()
{
int max = data[1];
exchange(1,N-1);
N--;
sink(1);
return max;
} int BinaryHeap::getMax()
{
return data[1];
}

main.c

#include <iostream>
#include "BinaryHeap.h" using namespace std; int main()
{
BinaryHeap * bp = new BinaryHeap(10);
bp->insert(3);
bp->insert(2);
bp->insert(1);
bp->insert(5);
bp->insert(8);
bp->insert(7);
cout << bp->getMax() << "\n";
bp->delMax();
cout << bp->getMax() << "\n";
char c;
cin >> c;
return 0;
}

各操作时间复杂度的分析:

2.堆排序

堆排序的主要步骤:

(1)建堆

(2)依次移除最大元素,将它放到数组的尾端。

3.各种排序算法的分析

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Algorithm Part I:Priority Queues的更多相关文章

  1. Retroactive priority queues

    http://erikdemaine.org/papers/Retroactive_TALG/paper.pdf 明天写..大概就是通过一些结论发现这个东西其实就是往最后的集合里加入或删除一些可以被快 ...

  2. C++ Priority Queues(优先队列) and C++ Queues(队列)

    C++优先队列类似队列, 但是在这个数据结构中的元素按照一定的断言排列有序. empty() 如果优先队列为空,则返回真 pop() 删除第一个元素 push() 加入一个元素 size() 返回优先 ...

  3. 算法第四版学习笔记之优先队列--Priority Queues

    软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...

  4. Thread Pool Engine, and Work-Stealing scheduling algorithm

    http://pages.videotron.com/aminer/threadpool.htm http://pages.videotron.com/aminer/zip/threadpool.zi ...

  5. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  6. c++ STL:队列queue、优先队列priority queue 的使用

    说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...

  7. find-median-from-data-stream & multiset priority queue 堆

    https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...

  8. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  9. DotNet 资源大全中文版(Awesome最新版)

    Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...

随机推荐

  1. 怎样将Emoj表情插入mysql5.6数据库__python+mysqldb

    废话不多说,相信看到这里的看客已经看过非常多配置文件的设置方法.可是问题还是没有解决.本文就具体记录一下我的解决方法吧. 我的环境:mysql5.6+python2.7.3+MySQLdb1.2.4 ...

  2. org.apache.hadoop.ipc.Client: Retrying connect to server异常的解决

    检查发现是DataNode一直连接不到NameNode. 检查各个节点在etc/hosts中的配置是否有127.0.1.1 xxxxxx.如果有把其屏蔽或者删除,重启各节点即可. 原因:127.0.1 ...

  3. 基于visual Studio2013解决面试题之0202上下排

     题目

  4. 谈谈Ext JS的组件——布局的用法续二

    绝对布局(Ext.layout.container.Absolute) 绝对布局让我回忆到了使用Foxpro开发的时候,哪时候的界面布局就是这样.通过设置控件的左上角坐标(x.y)和宽度来进行的,由于 ...

  5. Android应用开发之(通过ClipboardManager, ClipData进行复制粘贴)

    在开发一些系统应用的时候,我们会用到Android的剪贴板功能,比如将文本文件.或者其他格式的内容复制到剪贴板或者从剪贴板获取数据等操作.Android平台中每个常规的应用运行在自己的进程空间中,相对 ...

  6. spring获取bean 实例

    ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...

  7. 选择屏幕中的下拉框和dialog中下拉框设计

    REPORT  YTEST014. PARAMETERS: auart LIKE vapma-auart  AS LISTBOX   VISIBLE LENGTH 6. AT SELECTION-SC ...

  8. VC 绘图技巧--自定义形状图形

    自定义形状图形,定义几个点围城的图形,然后进行描边和填充: if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1 ...

  9. Linux Shell常用技巧(一) RE

    一.    特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文件,/dev/null和/dev/tty.其中/dev/null将会丢掉所有写入它 ...

  10. Phpcms V9 所有的中文变量

    $LANG['start_update_category'] = '开始更新栏目页 ...'; $LANG['start_to_end_id'] = '" 第{page} - {endpag ...