Heap Sort 

  1. Build a max heap using exsiting array, which is called Heapify
  2. Swap root with the last element, and re-Heapify the root node

Heapify

Heapify(array, n, i) = 1) compare node[i] with children   2)node[i] is already the largest one, no op.  3) child is largest one, swap, then Heapify(array, n, 2*i + 1 or 2i* + 2)

Code

    public class HeapSort
{
/// <summary>
/// Heapify the array
/// </summary>
/// <param name="array">array</param>
/// <param name="n">total size of the heap</param>
/// <param name="i">starting node</param>
public void Heapify(int[] array, int n, int i)
{
int left = *i + ;
int right = *i + ; int largest = i; if (left < n)
{
if (array[left] > array[largest]) // <---- 此处注意,要用array[largest] instead of array[i], 这样我们可以一直跟踪最大的下标, 而不会错误的交换次大的下标
{
largest = left;
}
} if (right < n)
{
if (array[right] > array[largest])
{
largest = right;
}
} if (largest != i)
{
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
Heapify(array, n, largest);
}
} public void BuildHeap(int[] array)
{
for(int i = array.Length / - ; i >= ; i--)
{
Heapify(array, array.Length, i);
}
} public void Sort(int[] array)
{
if (array == null || array.Length == )
{
return;
} BuildHeap(array); for(int i = array.Length - ; i >= ; i--)
{
Swap(ref array[], ref array[i]);
Heapify(array, i, );
}
} public void Print(int[] array)
{
for(int i = ; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
} private void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
}

Comments

  1. Heap sort is a in place sort.
  2. Time complexity is O(NLogN) for Heapify.A quick look over the above algorithm suggests that the running time is , since each call to Heapify costs  and Build-Heap makes  such calls.

Heap Sort - recursion的更多相关文章

  1. Insert or Merge && Insertion or Heap Sort

    原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102 题目如下: According to Wikipedia: Inserti ...

  2. [Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)

    [Unity][Heap sort]用Unity动态演示堆排序的过程 How Heap Sort Works 最近做了一个用Unity3D动态演示堆排序过程的程序. I've made this ap ...

  3. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. 09-排序3 Insertion or Heap Sort

    和前一题差不多,把归并排序换成了堆排序.要点还是每一次排序进行判断 开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误...一直在检查逻辑 建立最大堆 排 ...

  5. 堆排序 Heap Sort

    堆排序虽然叫heap sort,但是和内存上的那个heap并没有实际关系.算法上,堆排序一般使用数组的形式来实现,即binary heap. 我们可以将堆排序所使用的堆int[] heap视为一个完全 ...

  6. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)

    堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...

  8. 堆排序(Heap Sort)的C语言实现

    堆排序(Heap Sort)具体步骤为 将无序序列建成大顶堆(小顶堆):从最后一个非叶子节点开始通过堆调整HeapAdjust()变成小顶堆或大顶堆 将顶部元素与堆尾数组交换,此是末尾元素就是最大值, ...

  9. Heap Sort

    #include<iostream> using namespace std; const int MAX = 1001; int l[MAX]; //Heap Sort void Hea ...

随机推荐

  1. webapi put 404

    windows server 2016  IIS  webapi   404 error In IIS select your website and double-click Handler Map ...

  2. Factorial(hdu 1124)

    Description The most important part of a GSM network is so called Base Transceiver Station (BTS). Th ...

  3. Android直接用手机打包apk!

      你没有看错,用手机浏览器访问Jenkins,就可以打包apk,并生成下载二维码,发送邮件通知测试人员下载,从此解放双手,告别打包测试.先上本人手机邮箱收到的打包成功通知效果图:     废话少说, ...

  4. 《深入分析Java web技术内幕》读书笔记(一)

    1.什么时网站 网站就是利用Html工具制作用于展示特定内容的网页集合,网站也是一种软件. 网站的开发过程需要考虑其完整性.目的性.扩展性和安全性. 2.C/S架构跟B/S架构 C/S架构:客户端和服 ...

  5. git 的安装与初始化

    1搭建本地git服务器: 1.1安装git 对于ubuntu系统,一般自带git,可以使用git --version 查看版本号 ,或使用apt-get install git  . centos上对 ...

  6. swiper使用中一些点的总结

    最近做了PC端改版,要求移动端有更好的体验,一些产品滚屏的展示,就用了swiper插件,以方便用户在移动端访问可以滑动翻屏展示. 本次项目中使用的是swiper2.0版本. 首先要引入swiper的j ...

  7. UML类图中最重要的几种类关系及其表示

    阅读UML图最常见到的类与类之间的关系有如下几种: 1.依赖关系 依赖关系是指一个类在计算时,应用了“另一个类”类型的参数,这种关系是偶然.临时.弱的. UML类图中,依赖关系用带单箭头的虚线表示,即 ...

  8. 聚类--K均值算法

    import numpy as np from sklearn.datasets import load_iris iris = load_iris() x = iris.data[:,1] y = ...

  9. Python中的 *args 和 **kwargs

    基本概念 Python支持可变参数,最简单的方法莫过于使用默认参数. def test_defargs(one, two=2): # 参数one没有默认值,two的默认值为2 print('Requi ...

  10. ImportError: No module named _tkinter, please install the python-tk package ubuntu运行tkinter错误

    这是由于Python的版本没有包含tkinter的模块,只需要把tk的package安装就可以了. 一般在Linux才出现,windows版本一般已经包含了tkinter模块. apt-get ins ...