Heap Sort - recursion
Heap Sort
- Build a max heap using exsiting array, which is called Heapify
- 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
- Heap sort is a in place sort.
- 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的更多相关文章
- Insert or Merge && Insertion or Heap Sort
原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102 题目如下: According to Wikipedia: Inserti ...
- [Unity][Heap sort]用Unity动态演示堆排序的过程(How Heap Sort Works)
[Unity][Heap sort]用Unity动态演示堆排序的过程 How Heap Sort Works 最近做了一个用Unity3D动态演示堆排序过程的程序. I've made this ap ...
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 09-排序3 Insertion or Heap Sort
和前一题差不多,把归并排序换成了堆排序.要点还是每一次排序进行判断 开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误...一直在检查逻辑 建立最大堆 排 ...
- 堆排序 Heap Sort
堆排序虽然叫heap sort,但是和内存上的那个heap并没有实际关系.算法上,堆排序一般使用数组的形式来实现,即binary heap. 我们可以将堆排序所使用的堆int[] heap视为一个完全 ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...
- 堆排序(Heap Sort)的C语言实现
堆排序(Heap Sort)具体步骤为 将无序序列建成大顶堆(小顶堆):从最后一个非叶子节点开始通过堆调整HeapAdjust()变成小顶堆或大顶堆 将顶部元素与堆尾数组交换,此是末尾元素就是最大值, ...
- Heap Sort
#include<iostream> using namespace std; const int MAX = 1001; int l[MAX]; //Heap Sort void Hea ...
随机推荐
- Myeclipse在debug模式下没加断点程序卡住,start模式下可以正常启动
参考<eclipse在debug模式下卡住,start模式下可以启动>,地址:https://blog.csdn.net/jack_chen1994/article/details/761 ...
- 进到页面后input输入框自动获取焦点
<html> <head></head> <body> 用户名:<input type="text" ...
- Linux系统启动过程(通俗易懂)
前言: Linux是一种自由和开放源代码的类UNIX操作系统.该操作系统的内核由林纳斯·托瓦兹在1991年10月5日首次发布.在加上用户空间的应用程序之后,成为Linux操作系统.Linux是自由软件 ...
- NPOI 关于Excel的学习
1.传送门:http://blog.csdn.net/guo_lover/article/details/52399570
- week2
三元函数: a,b,c = 1,2,3 d = a if a>b else c print(d) #list 用法: lst = [1,2,3,4,5] print(lst[0:3]) prin ...
- fiddler 4 设置代理
Windows使用一种被称为“AppContainer”的隔离技术,它可能会干扰lmmersive应用程序和Edge浏览器的流量捕捉.使用进度Telerik Fiddler的工具栏上的WinConfi ...
- django中的Q查询
转载于:https://mozillazg.com/2015/11/django-the-power-of-q-objects-and-how-to-use-q-object.html 原文写的很详细 ...
- 一个简单的Quartz定时任务
package com.shuadan.quartz; import org.springframework.scheduling.annotation.Scheduled; import org.s ...
- 域名排序 sort uniq awk
[root@web01 ~]# sort [-fbMnrtuk] [file or stdin] 选项与参数:-f :忽略大小写的差异,例如 A 与 a 视为编码相同:-b :忽略最前面的空格符部分: ...
- centos7 下zookeeper 部署 单机多实例模式
centos7 下zookeeper 部署 本文参考https://www.linuxidc.com/Linux/2016-09/135052.htm 1.创建/usr/local/zookeeper ...