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. centos 7安装python 3

    linux-Centos7安装python3并与python2共存   1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的 ...

  2. jsp转发与重定向的区别

    1.转发的实现其实很简单,使用request的getRequestDispatch()方法得到RequestDispatch对象,然后在括号里放转发的地址,然后用这个对象调用forward()方法,里 ...

  3. 安装vue 教程

    安装教程:https://www.cnblogs.com/zhaomeizi/p/8483597.html

  4. VS2013中调驱动

    https://msdn.microsoft.com/en-us/library/windows/hardware/jj200334(v=vs.85).aspx 需要注意的就是 debugport:n ...

  5. S2T40,第五章

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Django REST framework---请求和响应

    Django REST framework---请求和响应 [Request对象] 概念: 平时我们在写Django的视图函数的时候,都会带上一个request参数,这样就能处理平时搭建网站时,浏览器 ...

  7. pandas 常用技巧总结

    切片: loc:df.loc[num]:选择df 某一行 seriesdf.loc[[num1,num2]]: 选择df 某几行df.loc[[True,False,True,   ,True]]: ...

  8. Python随笔--对象

    组合的用法:

  9. Java——总结

    一.编写并运行java程序步骤: 1.编写java源代码 java源代码文件都已java作为扩展名 java代码格式: class 类名{ //主方法} 2.编译,将字符文件编译为字节文件 在dos中 ...

  10. URL 通过Get方式传递数组参数

    URL 通过Get方式传递数组参数 方法1: ?id=1&id=2&id=3 后台获取时,只需要reqeust.getParameterValues("id") 获 ...