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. ajax入门学习

    1.   XMLHttpRequest 是 AJAX 的基础. 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject). XMLHttpReq ...

  2. web前端学习python之第一章_基础语法(一)

    web前端学习python之第一章_基础语法(一) 前言:最近新做了一个管理系统,前端已经基本完成, 但是后端人手不足没人给我写接口,自力更生丰衣足食, 所以决定自学python自己给自己写接口哈哈哈 ...

  3. Daily record-November

    November 11. I managed to grab her hand. 我抓到了她的手.2. He passed a hand wearily over his eyes. 他疲倦地用手抹了 ...

  4. SqlSugar GridView aspnetpager 分页排序

    HTML页面 <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefi ...

  5. 颠覆区块链的钱包——AngelToken钱包

    币圈的玩家/大佬们的刚需产品Angel token钱包您有吗?    当前由法国凡赛公司技术团队研发的去中心化AngelToken智能钱包,它兼容比特币底层技术系列币种,同时也兼容以太坊底层技术系列币 ...

  6. C++学习笔记(一):C++基础知识

    一.C++基础知识 新的数据类型 C语言中的数据类型 C++中新的数据类型 思考:新的数据类型有什么好处?请看下面的代码: 可以见得:新的类型使整个程序更加简洁,程序变得易读易懂!这个就是bool类型 ...

  7. jqgrid自定义列表开发=》实现高级查询

    标题已指出本文要说的三件事,首先是主角jqgrid,将应用在自定义列表中,重点介绍如何实现高级查询. 使用jqgrid实现自定义列表分为两大步骤,首先是要根据业务完成jqgrid的对象定义,即列表的描 ...

  8. Python面向对象基础知识

    面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用 ...

  9. 链接中 href='#' 和 href='###' 的区别

    <a> 标签 + onclick='{jscode}' 是很常用的一种 js 运用方式,而不使用 href='javascript:{jscode}' 是为了兼容多种浏览器对 <a& ...

  10. react native进一步学习(NavigatorIOS 学习)

    特别申明:本人代码不作为任何商业的用途,只是个人学习的一些心得,为了使得后来的更多的程序员少走一些弯路.*(如若侵犯你的版权还望见谅)*. 开发工具:WebStorm,xcode 1. rn的创建的时 ...