堆排序利用的是堆这种数据结构来对进行排序,(二叉)堆可以被视为一棵完全的二叉树,树的每个节点与数组中存放该节点的值得那个元素对应。这里使用最大堆进行排序算法设计,最大堆就是parent(i) > leftchild(i) 且parent(i) > rightchild(i),首先利用迭代法进行建堆。

int left(int index)
{
return index*+;
}
int right(int index)
{
return index*+;
}

下面是建堆的函数:

void MaxHeapify(int *a, int node, int iHeapSize)
{
int iIndexL = left(node);
int iIndexR = right(node);
int iLargest = node;
if(iIndexL < iHeapSize && a[iIndexL] > a[node])
{
iLargest = iIndexL;
}
else
iLargest = node;
if(iIndexR < iHeapSize && a[iIndexR] > a[iLargest])
{
iLargest = iIndexR;
}
if(iLargest != node)
{
swap(a[iLargest], a[node]);
MaxHeapify(a, iLargest, iHeapSize);
} }
void BuildHeap(int *a, int &iHeapSize)
{
int iSize = iHeapSize;
for(int iLoop = iSize/-; iLoop != ; --iLoop)
{
MaxHeapify(a, iLoop, iHeapSize);
}
}

以上代码可以建立一个最大堆,在子数组中A[n/2+1 .. n]中的元素都是树的叶子节点,可以看作是只含一个元素的堆,因此只需用BuilHeap对树中的其他节点调用MaxHeapify
来建立最大堆。

接着就是排序,因为最大元素在A[1](为了避免节点计算麻烦,序号从1开始),可以通过将它与A[n]交换。调用MaxHeapify(a, 1, iHeapSize)来保持最大堆性质,然后重复这个过程,堆的大小由n-1一直降到1.排序部分如下:

void HeapSort(int *a, int heapSize)
{
if(a == NULL)
{
return;
} BuildHeap(a, heapSize);
for(int i=heapSize-; i>=; i--)
{
swap(a, a+i);
--heapSize;
MaxHeapify(a, , heapSize);
} }

algorithm ch6 heapsort的更多相关文章

  1. algorithm ch6 priority queque

    堆数据结构的一个重要用处就是:最为高效的优先级队列.优先级队列分为最大优先级队列和最小优先级队列,其中最大优先级队列的一个应用实在一台分时计算机上进行作业的调度.当用堆来实现优先级队列时,需要在队中的 ...

  2. POJ 2166 Heapsort(递推)

    Description A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n l ...

  3. Java HeapSort

    Java HeapSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational & ...

  4. Java算法-堆排序

    package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; public class HeapSor ...

  5. Java算法-希尔排序

    希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题.希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, ...

  6. Java中各种排序算法

    package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treero ...

  7. 用Java实现几种常见的排序算法

    用Java语言实现的各种排序,包括插入排序.冒泡排序.选择排序.Shell排序.快速排序.归并排序.堆排序.SortUtil等. 插入排序: package org.rut.util.algorith ...

  8. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

  9. heapsort

    Introduction to Algorithms Third Edition The (binary) heap data structure is an array object that we ...

随机推荐

  1. Android当代码方法超过65536个时,在2.3机器上会不能安装,出现INSTALL_FAILED_DEXOPT错误

    今天打包时,发现2.3机器,产生的APK在某些机器上不能安装(Installation error: INSTALL_FAILED_DEXOPT),针对这个问题的一个可能解释是:最新的ADT和SDK ...

  2. CTS测试笔记

    电脑安装12.4乌班图系统 更新源 (1) 打开ubuntu software center (2) 电脑左上角选择edit→software sources…→点击download from,选择o ...

  3. Linux下的调试工具

    Linux下的调试工具 随着XP的流行,人们越来越注重软件的前期设计.后期的实现,以及贯穿于其中的测试工作,经过这个过程出来的自然是高质量的软件.甚至有人声称XP会淘汰调试器!这当然是有一定道理的,然 ...

  4. 杀死 tomcat 进程的脚本

    新建一个.sh 文件 把下面的内容复制进去.然后 把这个文件放到tomcat 的bin目录下在关闭tomcat 执行这个脚本. 可以解决 在关闭tomcat的时候 总是遗留一些tomcat进程没有结束 ...

  5. CodeForces-1121C System Testing

    题目链接 https://vjudge.net/problem/CodeForces-1121C 题面 Description Vasya likes taking part in Codeforce ...

  6. 多版本python import 问题解决方案

    原文http://www.tuicool.com/articles/EnE7nm6 多版本Python共存[支持使用pip安装包] 有时特殊需要会要用到高版本的Python, 但是系统自带的版本又是很 ...

  7. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  8. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  9. NO8——排序

    //sort #include<algorithm> bool cmp(const int a,const int b) { return a>b;//降序排列 } //qsort ...

  10. 自定义Json格式

    老铁们都知道,一般的json格式就是键值对格式,在一些特定的框架或者系统中,会用到自定义格式的json文件,假设我们要得到的特定格式json格式如下: {"A":"2&q ...