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.pswp.cn/news/542866.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542866.shtml
英文地址,請注明出處:http://en.pswp.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的幾個細節…