Algorithm Part I:Priority Queues
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的更多相关文章
- Retroactive priority queues
http://erikdemaine.org/papers/Retroactive_TALG/paper.pdf 明天写..大概就是通过一些结论发现这个东西其实就是往最后的集合里加入或删除一些可以被快 ...
- C++ Priority Queues(优先队列) and C++ Queues(队列)
C++优先队列类似队列, 但是在这个数据结构中的元素按照一定的断言排列有序. empty() 如果优先队列为空,则返回真 pop() 删除第一个元素 push() 加入一个元素 size() 返回优先 ...
- 算法第四版学习笔记之优先队列--Priority Queues
软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...
- Thread Pool Engine, and Work-Stealing scheduling algorithm
http://pages.videotron.com/aminer/threadpool.htm http://pages.videotron.com/aminer/zip/threadpool.zi ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
- c++ STL:队列queue、优先队列priority queue 的使用
说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...
- find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- DotNet 资源大全中文版(Awesome最新版)
Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...
随机推荐
- HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- Android 最火高速开发框架AndroidAnnotations使用具体解释
Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤,Android 最火高速开发框架AndroidAnnotations简介文章中的简介, ...
- 499 - What's The Frequency, Kenneth?
What's The Frequency, Kenneth? #include <stdio.h> main() { int i; char *suffix[]= { "st ...
- Swift - 将String类型的数字转换成数字类型
Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = (tf1.text as N ...
- 自己定义android 4.0以上的对话框风格
做个笔记.这里是Dialog的风格,假设是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...
- 给你的站点加入 console.js
本文仅先给使用console调试的FE同学,假设你还不知道console是什么.或者还停留在alert阶段,那就不要浪费时间了,say bay bay! 你是否试程序的过程中用过console.log ...
- 配置虚拟主机并更改Apache默认解析路径
配置虚拟主机,非常easy 改动以下文件: 加入以下几句话 <VirtualHost *:80> ##ServerAdmin webmaster@dummy-host2.example.c ...
- Oauth认证的时候报错:timestamp_refused
今天server大规模报错,大部分用户无法登陆,小部分能够登陆,非常是奇怪. 查看log.调试代码,发现问题是在oauth认证的时候出了问题,报 timestamp_refused. google了下 ...
- 【m从翻译os文章】写日志禁令Sqlnet.log和Listener.log
写日志禁令Sqlnet.log和Listener.log 参考原始: How to Disable Logging to the Sqlnet.log and the Listener.log (Do ...
- Servlet和JSP读书笔记(一)
Java Servlet 技术,简称Servlet,是Java中用于开发web应用程序的基本技术. Servlet实际上也就是一个Java程序.一个Servlet应用程序通常包含很多Servlet.而 ...