java 实现 堆排序算法

Heap Sort is a comparison-based sorting algorithm that makes use of a different data structure called Binary Heaps. Let us understand some important terms,

堆排序是一种基于比较的排序算法,该算法利用称为二进制堆的不同数据结构。 让我们了解一些重要的术语,

  1. Complete Binary Tree: A tree is complete when all the levels (except of course the last level) are completely filled, i.e. all the parent nodes have two children nodes each and all the nodes are as far left as possible which means first we fill the left node and then the right.

    完整的二叉树 :当所有级别(当然,最后一个级别除外)都被完全填充时,即所有树的父节点都有两个子节点,并且所有节点都尽可能靠左,这意味着一棵树完成了。左节点,然后右。

  2. Binary Heap: It is a complete binary tree but there is an order of elements from parent to children in Binary Heap. They can be of two types,

    Binary Heap :这是一个完整的二叉树,但是Binary Heap中从父级到子级都有一个元素顺序。 它们可以有两种类型,

    1. Max Binary Heap or Max Heap where the parent node is greater than its two children nodes.
    2. 最大父节点大于其两个子节点的最大二进制堆或最大堆
    3. Min Binary Heap or Min Heap where the parent node is smaller than its children nodes.最小二进制堆或最小堆 ,其中父节点小于其子节点。

The main() function of Heap Sort is to call the heapify() function which leads to the building of a max heap and then the largest element is stored in the last position of the array as so on till only one element is left in the heap. Array representation of heap is preferred because it occupies less space and also it is easy to reference the root and children nodes.

堆排序main()函数将调用heapify()函数,该函数将导致建立最大堆,然后将最大元素存储在数组的最后一个位置,依此类推,直到在数组中只剩下一个元素为止。堆。 堆的数组表示形式是首选的,因为它占用较少的空间,并且易于引用根节点和子节点。

Algorithm (Considering Max heap):

算法(考虑最大堆):

  1. First, we form a Max Heap such that the first node or the root node is the largest element. This step takes O(N) time complexity.

    首先,我们形成一个最大堆,以使第一个节点或根节点成为最大元素。 此步骤需要O(N)时间复杂度。

  2. Next, we swap the root element with the last element of the heap and reduce the size of heap by 1.

    接下来,我们将根元素与堆的最后一个元素交换,并将堆的大小减小1。

  3. Repeat steps 1 and 2 are till only 1 element is left.

    重复步骤1和2,直到仅剩1个元素。

How to build the Heap?

如何建立堆?

The heapify procedure can be applied to a node only when its children nodes are heapified. Therefore, we start the heapification with the last non-leaf node. To find the first non-leaf node, the following formula is used: First non-leafy node = lower bound (n/2)

仅当对子节点进行堆化时,才能将heapify过程应用于该节点。 因此,我们从最后一个非叶子节点开始堆化 。 要找到第一个非叶子节点,请使用以下公式: 第一个非叶子节点=下界(n / 2)

Hence, if there are 5 elements in the heap, the first non-leafy node would be the second node or the node at index 1.

因此,如果堆中有5个元素,则第一个非叶节点将是第二个节点或索引为1的节点。

Pseudo Code:

伪代码:

Heap_Sort (arr[], n)
{
// Creating the initial Max heap
for i = n/2 – 1 to 0:
heapify(arr, n, i)
// Swapping largest element and repeating the steps further
for i = n-1 to 0:
swap(arr[0], arr[i]
heapify(arr, n, i)
}
Heapify (arr[], n, i)
{
int largest = i;
int left = 2*i + 1; // Left child
int right = 2*i + 2; // Right child
// Check if left child exists and is larger than root
If (left < n && arr[left] > arr[largest]):
Largest = left;
// Check if right child exists and is larger than largest
If (right < n && arr[right] > arr[largest]):
largest = right;
// Change root, if root is not the largest
If(largest != i)
Swap(arr[i], arr[largest])
Heapify(arr, n, largest); //Repeat till max heap is obtained
}

Time Complexity:

时间复杂度:

The time complexity of Heap sort is:

堆排序的时间复杂度为:

  1. Worst Case = O(N log N)

    最坏情况= O(N log N)

  2. Average Case = Ɵ(N log N)

    平均情况=Ɵ(N log N)

  3. Best Case = Ω(N log N)

    最佳情况=Ω(N log N)

  4. Space Complexity: Ɵ(1)

    空间复杂度:Ɵ(1)

The time complexity of Heapify is O(log N) and that of Build_heap / Heap_Sort is O(N). The overall complexity of Heap_Sort is therefor, O(N log N).

Heapify的时间复杂度为O(log N),而Build_heap / Heap_Sort的时间复杂度为O(N)。 因此,Heap_Sort的总体复杂度为O(N log N)。

Heap Sort Implementation:

堆排序实现:

#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(int arr[], int n, int i)
{
int left, right, largest;
largest = i;
left = 2 * i + 1;
right = 2 * i + 2;
// Check if left child exists and is larger than its parent
if (left < n && arr[left] > arr[largest])
largest = left;
// Check if right child exists and larger than its parent
if (right < n && arr[right] > arr[largest])
largest = right;
// if root is not the largest
if (largest != i) {
swap(&arr[i], &arr[largest]); //make root the largest
heapify(arr, n, largest); // Apply heapify to the largest node
}
}
void heap_sort(int arr[], int n)
{
int i;
for (i = (n / 2) - 1; i >= 0; i--)
heapify(arr, n, i);
for (i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]); //Move the largest element at root to the end
heapify(arr, i, 0); //Apply heapify to reduced heap
}
}
int main()
{
int arr[] = { 20, 13, 34, 56, 12, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
heap_sort(arr, n);
printf("\nAfter performing Heap Sort:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

Output:

输出:

Array:
20 13 34 56 12 10
After performing Heap Sort:
10 12 13 20 34 56

Applications:

应用范围:

  1. Job Scheduling: In Linux OS is used due to low space and time complexity

    作业调度 :由于空间和时间复杂度低,在Linux OS中使用

  2. Graph Algorithms: Djikstar's Algorithm, Prim's Algorithm and Huffman Coding

    图算法 :Djikstar算法,Prim算法和霍夫曼编码

翻译自: https://www.includehelp.com/c-programs/implement-heap-sort-algorithm.aspx

java 实现 堆排序算法

java 实现 堆排序算法_C程序实现堆排序算法相关推荐

  1. java c语言 实现des算法_C语言实现DES算法

    /*------------------------------------------------------- Data Encryption Standard 56位密钥加密64位数据 2011 ...

  2. eq值 推荐算法_C++实现十种排序算法

    十种排序算法: 选择排序 插入排序 冒泡排序 希尔排序 快速排序的三种实现方法 归并排序 堆排序(大顶堆) 计数排序 基数排序(待实现) 桶排序(待实现) #include <bits/stdc ...

  3. 计算机算法与程序设计基础知识,算法与程序设计试题带答案

    高一第二学期<算法与程序设计>学分认定试题 学校:_____________ 班级:____________ 学号:____________ 姓名:____________ 一.单选题(每 ...

  4. bfgs算法matlab程序,BFGS优化算法及应用实例.docx

    PAGE 目 录 TOC \o "1-3" \h \z \u HYPERLINK \l "_Toc282785128" 1.引言 PAGEREF _Toc282 ...

  5. c语言排序算法_C语言写排序算法(二) —— 选择排序

    上次联系的算法是冒泡排序,不知道小伙伴们还记得不,中间穿插了一下比特币相关的分享,嘿嘿.今天来分享一下选择排序. 算法描述:选择排序是从数组中选择最大(小)的元素放到数组的最后一个,然后往前移,接着从 ...

  6. 分类算法python程序_分类算法——k最近邻算法(Python实现)(文末附工程源代码)...

    kNN算法原理 k最近邻(k-Nearest Neighbor)算法是比较简单的机器学习算法.它采用测量不同特征值之间的距离方法进行分类,思想很简单:如果一个样本在特征空间中的k个最近邻(最相似)的样 ...

  7. pso算法matlab程序,基本PSO算法的matlab源程序

    <基本PSO算法的matlab源程序>由会员分享,可在线阅读,更多相关<基本PSO算法的matlab源程序(2页珍藏版)>请在人人文库网上搜索. 1.主函数源程序( main. ...

  8. md5与des算法有何不同_Python算法详解:为什么说算法是程序的灵魂?

    算法是程序的灵魂,只有掌握了算法,才能轻松地驾驭程序开发.软件开发工作不是按部就班,而是选择一种最合理的算法去实现项目功能.算法能够引导开发者在面对一个项目功能时用什么思路去实现,有了这个思路后,编程 ...

  9. java程序员基础算法_java程序员必知的十种程序算法

    <java程序员必知的十种程序算法>由会员分享,可在线阅读,更多相关<java程序员必知的十种程序算法(11页珍藏版)>请在人人文库网上搜索. 1.java程序员必学的十种程序 ...

最新文章

  1. Linux Kernel TCP/IP Stack — L3 Layer — 邻居发现子系统
  2. boostrap-table export 导出监听
  3. 可降阶的高阶微分方程
  4. android+rom+bootloader+flash,Android ROM开发(4) bootloader 三种启动模式
  5. Snap svg 主要对象
  6. PLC的模糊PID控制
  7. Sakai 2.6 配置
  8. Fibonacci Heaps
  9. Arduino 利用ADS1115 AD转换模块进行数据读取
  10. 让QQ群昵称色变的神奇代码
  11. 企业WiFi认证,怎么确保企业WiFi安全?
  12. RH2288v3常用的知识
  13. 《算法笔记》学习笔记(1)
  14. Alignedreid项目源码分析
  15. java基础零散知识
  16. java中设置基偶隔行换色_Excel2016中奇偶行填充不同颜色的方法
  17. director入门
  18. 第一学历和最高学历哪个更重要?
  19. MacBook Pro m1 homebrew 安装,卸载脚本
  20. 武汉大学计算机学院的博士后,武汉大学经济与管理学院

热门文章

  1. HTML5中volume样式自定义,html5中关于volume属性的使用详解
  2. http 错误 404.0 - not found_电脑Regsvr32 用法和错误消息的说明
  3. curl 的用法指南
  4. linux中权限765啥意思,Linux中的文件权限
  5. 怎样查询2021高考模拟成绩,2021年高三一模二模三模哪个成绩更接近高考成绩,看看网友是怎么评论...
  6. python 远程控制_用 Python 远程控制你的电脑
  7. java方法的参数类型_Java 基础 14 方法的重载 与 方法参数类型详解
  8. java期末考试2013及答案_java笔试经典(题及答案)2013.doc
  9. snort的详细配置
  10. 关于java.util.ConcurrentModificationException和remove倒数第二个元素