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. [置顶] 用mootools实现checkbox全选功能

    全选时,所有的单个checkbox都要选中,反过来也可以实现 //全选按钮 $('chkall').addEvent('click',function(){ $$('input[name=" ...

  2. UVA它11292 - Dragon of Loowater

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  3. list view Item 里面有ImageButton

    list view Item 里面有ImageButton listview 的item中有ImageButton, 或者 Button. 目的: ImageButton 和 Button的click ...

  4. HDU 4344 随机法判素数(费马小定理

    #include <cstdio> #include <ctime> #include <cmath> #include <algorithm> usi ...

  5. 利用iframe技巧获取訪问者qq

    今天工作时,有个暂时加的好友问我,怎么利用web页面获取訪问者的qq. 曾经没有接触过,感觉到非常好奇,可是工作中脑子非常亢奋,转的快,利用所学的知识迅速想到一条技巧,那就是假想用户在进入我们设定的页 ...

  6. AS3开发必须掌握的内容

    1.事件机制 2.显示列表 3.垃圾回收 4.常用方法 5.网络通信 6.位图动画 7.渲染机制 8.API结构 9.沙箱机制 10.资源管理 11.内存管理 12.性能优化 13.资源选择 14.安 ...

  7. cocos2dx触屏响应(单点触摸)CCTouchBegan,CCTouchMove,CCTouchEnd

    今天白白跟大家分享一下cocos2dx单点触摸经验. cocos2dx触摸CCTouch类的单点触摸有四个函数CCTouchBegan,CCTouchMove,CCTouchEnd,CCTouchCa ...

  8. 一个跨平台的 C++ 内存泄漏检测器

    2004 年 3 月 01 日 内存泄漏对于C/C++程序员来说也可以算作是个永恒的话题了吧.在Windows下,MFC的一个很有用的功能就是能在程序运行结束时报告是否发生了内存泄漏.在Linux下, ...

  9. QT 多线程程序设计(也有不少例子)

    QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...

  10. 获取option中间的值

    <select name="wytype" id="wytype"> <option value="">—请选择—& ...