java 实现 堆排序算法_C程序实现堆排序算法

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.最小二进制堆或最小堆 ,其中父节点小于其子节点。
Heap Sort

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 实现 堆排序算法

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542866.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

嵌入式linux面试题解析(四)——逻辑推理一

嵌入式linux面试题解析&#xff08;四&#xff09;——逻辑推理一1、谁是罪犯问题一位法官在审理一起盗窃案时&#xff0c;对涉及到的四名嫌疑犯A、B、C、D进行了审问。四人分别供述如下&#xff1a;A&#xff1a;“罪犯在B、C、D三人之中。”B&#xff1a;“我没有作案&#x…

linux rsa登录改密码登录_LINUX中RSA认证登录SSH(不需要输入密码登录)2种方法

方法一&#xff0c;有的时候经常需要登录ssh&#xff0c;每次都需要输入密码&#xff0c;会比较繁琐。所以设置了一下使用RSA公钥认证的方式登录Linux。首先需要在服务器端设置/etc/ssh/sshd_config# vim /etc/ssh/sshd_config修改如下两行为yes。其实大多数情况下不用修改&…

b+树时间复杂度_数据结构:线性表,栈,队列,数组,字符串,树和二叉树,哈希表...

作者&#xff1a;张人大代码效率优化复杂度 -- 一个关于输入数据量n的函数时间复杂度 -- 昂贵与代码的结构设计有着紧密关系一个顺序结构的代码&#xff0c;时间复杂度是O(1), 即任务与算例个数 n 无关空间复杂度 -- 廉价与数据结构设计有关数据结构 -- 考虑如何去组织计算机中…

figure服务器无法显示,求大神帮帮忙,看一下为什么第二个figure出不来,只能显示第一个...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼Iimread(C:\Users\Administrator\Desktop\123.jpg);figure(1)subplot(3,2,1),imshow(I), title(原始图像);I1rgb2gray(I);subplot(3,2,2),imshow(I1),title(灰度图像);I2edge(I1,roberts,0.09,both);subplot(3,2,3),imshow(I2),tit…

python 示例_带有示例的Python File read()方法

python 示例文件read()方法 (File read() Method) read() method is an inbuilt method in Python, it is used to read the content of the file, by using this method we can read the specified number of bytes from the file or content of the whole file. read()方法是…

评价最高影片JAVAlibrary_视频 | 手游大神,动画导演,机圈新贵,极客怎么评价《愤怒的小鸟2》?...

谁能想到&#xff0c;迄今为止口碑最好的「游戏改编电影」竟然来自一个手机游戏IP&#xff1f;&#xff01;《愤怒的小鸟2》是有史以来评价最好的游戏改编电影。—— http://Screencrush.com《愤怒的小鸟2》凭什么能在打分平台上获得游戏改编电影最高分&#xff1f;—— http:/…

如何安装_如何安装吸顶灯?吸顶灯安装注意事项

摘要&#xff1a;灯是我们每个家庭都有的照明装置&#xff0c;它的造型和光能效果能直接影响到家居生活的氛围、美观度以及健康状况。吸顶灯的造型功能也随着科技的发展在不断发生多元化的改变。如今市面上的吸顶灯既有简单的装置又不比吊灯少了时尚奢华&#xff0c;让在层高较…

win10虚拟网络服务器,win10 虚拟专用网络服务器配置

win10 虚拟专用网络服务器配置 内容精选换一换本节将介绍在华为云关系型数据库服务的管理控制台创建实例的过程。目前&#xff0c;RDS for SQL Server支持“包年/包月”和“按需计费”购买&#xff0c;您可以根据业务需要定制相应计算能力和存储空间的华为云关系型数据库实例。…

scala中的二维数组_Scala中的多维数组

scala中的二维数组多维数组 (Multi-dimensional arrays) An Array that stores data in the form multidimensional matrix. Multidimensional arrays are generally used for making matrices and tables in programming. 一个以多维矩阵形式存储数据的数组 。 多维数组通常用…

easyui的textbox实现编辑保存_第80讲:工作表数据与UserForm窗口的交互,记录的编辑和保存...

大家好&#xff0c;我们今天继续讲解VBA数据库解决方案&#xff0c;今日讲解的是第80讲:工作表数据与UserForm窗口的交互过程中&#xff1a;如何对显示的记录进行编辑和保存。在前几讲中&#xff0c;我们实现了将工作表的数据传给UserForm窗口&#xff0c;实现的开始记录、下一…

jsp管理系统页面模板_jsp+ssh(spring+struts2+hibernate)+mysql实现的高校实验室管理系统...

今天给大家演示的是一款由jspssh(springstruts2hibernate)mysql实现的高校实验室管理系统本系统后端采用ssh框架&#xff0c;前端采用bootstrap和layui框架&#xff0c;界面美观大气。主要实现的功能有&#xff1a;1&#xff1a;教师和学生登录注册(超级管理员内置)。2&#xf…

aiml_AIML的完整形式是什么?

aimlAIML&#xff1a;人工智能标记语言 (AIML: Artificial Intelligence Markup Language) AIML is an abbreviation of "Artificial Intelligence Markup Language". AIML是“人工智能标记语言”的缩写 。 It is an XML dialect for making and producing natural …

小程序服务器获取appid,微信小程序小程序appid如何获取

经常有人问微信小程序的appid如何获取&#xff1f;小程序appid是小程序对应的id&#xff0c;通过小程序后台可以简单查询到。1、如果这个小程序是你做的小程序管理员进入公众平台、使用小程序帐户登录后&#xff0c;点击左侧菜单中的「设置」&#xff0c;在「开发设置」一项&am…

kailinux mysql提权_linux下mysql提权

linux提权,本文为您讲述一种linux提权方法&#xff0c;这是一种常见的linux提权技术..linux系统环境下&#xff0c;mysql以root权限登录时提权mysql5.x 的linux版本下面有一个函数&#xff0c;可以帮助我们干很多猥琐的事情&#xff0c;这个函数4。x下面貌似没有&#xff0c;原…

电脑模拟器哪个好_电脑系统杀毒软件哪个好测评

如果你不知道选择哪个杀毒软件的话&#xff0c;今天笔者就来告诉你杀毒软件哪个好&#xff0c;一起来看看杀毒软件排行榜吧。1、360杀毒。该软件拥有木马查杀、清理插件、漏洞修复、电脑体检等等多种功能。2、金山毒霸。该软件融合了启发式搜索、代码分析和虚拟机病毒查找等技术…

avr flash_AVR | USART家庭自动化

avr flashThe Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) is a highly flexible serial communication device. The main features are: 通用同步和异步串行接收器和发送器(USART)是一种高度灵活的串行通信设备。 主要特点是&#xff1a…

diskgenius 数据迁移_活见鬼,明明删除了数据,空间却没减少! - *IT界农民工*

迁移数据常用1、导出文件 - mysqldump 命令 ‍mysqldump 是 Mysql 自带的逻辑备份工具。其备份原理是通过协议连接到 Mysql 数据库&#xff0c;将需要备份的数据查询出来转换成对应的 insert 语句。当需要还原这些数据时&#xff0c;只要执行这些 insert 语句&#xff0c;即可将…

单片机小精灵t2_搭建S5P4418 ARM环境下 GPU OPENGL ES开发环境(适用 NANOPI2,3,M2,M3,T2,T3)...

本帖最后由 3guoyangyang7 于 2017-8-20 22:38 编辑先说一下背景&#xff0c;这几天做一个摄像头处理的qt项目&#xff0c;摄像头的像素是1280*720的&#xff0c;25fps&#xff0c;用qt的painter重绘widget窗体&#xff0c;会出现大量占cpu的情况&#xff0c;在刷新图片的时候整…

求出数组中元素的总和_数组中所有元素的总和可被给定数K整除

求出数组中元素的总和This program will help to find out the sum of elements in an array which is divisible by a number K. It uses the basic concept of modulo % or the remainder of a number. 该程序将帮助找出数组中被数字K整除的元素之和 。 它使用“&#xff05…

iphone短信尚未送达_第五期:从苹果 乔布斯 iPhone 说到张小龙 微信 理财通

这篇评测我是怀着敬畏之心写的。第一部分&#xff1a;从设计理念说起(一)说到设计理念&#xff0c;不得不先说下苹果的iPhone一)第一代iPhone于2007年1月9日由苹果公司前首席执行官史蒂夫乔布斯发布&#xff0c;并在2007年6月29日正式发售。让我们看一下第一代iPhone的几个细节…