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. HDU 4669 Mutiples on a circle (DP , 统计)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个环,每个点是一个数字,取一个子串,使 ...

  2. Apriori算法实现

    Apriori算法原理:http://blog.csdn.net/kingzone_2008/article/details/8183768 import java.util.HashMap; imp ...

  3. 利用开源HTML5引擎lufylegend.js结合javascript实现的五子棋人机对弈

    前言     本文主要介绍利用开源引擎 lufylegend.js开发基于Html5的游戏--五子棋,主要叙述其详细开发过程. 游戏规则 玩过五子棋的都应该知道五子棋的规则,这里就简单介绍其规则. 1 ...

  4. Ubuntu通过源代码编译安装Octave 4.0

    本教程/笔记,意在指导在Ubuntu及其它Linux系统上怎样通过源代码安装Octave. Octave简单介绍 Octave是GNU旗下取代matlab的数学工具软件,语法与matlab高度兼容.而 ...

  5. Java反射机制的使用方法

    Java的反射机制同意你在程序执行的过程中获取类定义的细节.有时候在程序执行的时候才得知要调用哪个方法,这时候反射机制就派上用场了. 获取类 类的获取方法有下面几种: forName().通过Clas ...

  6. VS2012配置astyle格式化代码

    1.工具->扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本. 2.工具->选项->AStyle Formatter->Edit,填入下面的,点击save,确定. ...

  7. [C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>

    总结 /* A.头文件<ctime> #if _GLOBAL_USING && !defined(RC_INVOKED) _STD_BEGIN 1.四个数据类型 using ...

  8. setjmp和longjmp函数使用详解

    源地址:http://blog.csdn.net/zhuanshenweiliu/article/details/41961975 非局部跳转语句---setjmp和longjmp函数.非局部指的是, ...

  9. timeout connect 10000 # default 10 second time out if a backend is not found

    timeout connect <timeout> timeout contimeout <timeout> (deprecated) Set the maximum time ...

  10. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...