C語言排序之?堆排序
前言:
堆是具有以下性質的完全二叉樹
每個節(jié)點大于或等于其左右子節(jié)點,此時稱為大頂(根)堆
?每個節(jié)點小于或等于其左右子節(jié)點,此時稱為小頂(根)堆
完全二叉樹在數組中下標換算公式
假設堆根節(jié)點從1開始編號(從1開始方便計算,0下標空著)
下面以編號為i的非根節(jié)點為例,計算其關聯節(jié)點的下標公式為:
其父節(jié)點:i/2
其左孩子:2i
其右孩子:2i+1
注:把這個完全二叉樹按層序遍歷放入數組(0下標不用),則滿足上面的關系表達
代碼工作流程
整體流程
a. 根據節(jié)點換算公式先從最下層非葉節(jié)點開始,依次從右到左(自下而上)一直到根創(chuàng)建初始堆
b. 循環(huán)n-1次,依次執(zhí)行:條件判斷后交換堆頂和堆尾元素
重建除堆尾外元素為新堆,一直到根
重建堆函數流程
接收參數開始下標和數組有效長度
保存堆頂,自上而下建堆,如果堆頂(臨時堆頂)比子節(jié)點?。ù箜敹阎校?,則孩子賦值給臨時堆頂位置(不需要swap函數交換,swap沒必要),并讓臨時堆頂位置指定子節(jié)點
for循環(huán)終止一定會找到合適的位置,此時臨時堆頂指向的位置可能是函數調用時的位置,也可能發(fā)生改變(代碼中執(zhí)行了一次強制賦值)
大小頂堆使用場景
大頂堆用來做升序排序,小頂堆用來做降序排序
時間復雜度
O(nlogn)
不穩(wěn)定
代碼
#include <stdio.h> #include <stdbool.h> #define MAXSIZE 9 typedef struct { int r[MAXSIZE+1]; // first index used as tmp, not real data int len; }SqList; void swap(SqList *L, int i, int j) { int tmp = L->r[i]; L->r[i] = L->r[j]; L->r[j] = tmp; } void heap_adjust(SqList *L, int s, int len) { int temp, i; temp = L->r[s]; // s(start) index may be a invalid element in this heap and try adjust for (i=s*2; i<=len; i*=2) { // compare with child if (i<len && L->r[i] < L->r[i+1]) { i++; // select the max child } if (temp >= L->r[i]) { break; // need not adjust } L->r[s] = L->r[i]; //need not swap, because always use temp compare with next level child s = i; // next loop, now s sub tree root node may be a invalid element } L->r[s] = temp; // finally, must be found the right place(or not changed) } void heap_adjust_2(SqList *L, int s, int len) { printf("use test function\n"); int temp, i; temp = L->r[s]; // s(start) index may be a invalid element in this heap and try adjust for (i=s*2; i<=len; i*=2) { // compare with child if (i<len && L->r[i] < L->r[i+1]) { i++; // select the max child } if (temp >= L->r[i]) { break; // need not adjust } swap(L, s, i); //need not swap, because always use temp compare with next level child s = i; // next loop, now s sub tree root node may be a invalid element } L->r[s] = temp; // finally, must be found the right place(or not changed) } void heap_sort(SqList *L) { // init serial to a heap first(type: big top), down->up and right->left int i, j; for (i=L->len/2; i>0; --i) { heap_adjust(L, i, L->len); //heap_adjust_2(L, i, L->len); } for (j=L->len; j>1; --j) { swap(L, 1, j); heap_adjust(L, 1, j-1); } } int main(void) { SqList list = { {999,50,10,90,30,70,40,80,60,20}, MAXSIZE }; heap_sort(&list); printf("after heap_sort:\n"); for (int i=0; i<=MAXSIZE; i++) { printf("index: %d, value: %d\n",i,list.r[i]); } return 0; }
output
? c gcc sort_heap.c && ./a.out
after heap_sort:
index: 0, value: 999
index: 1, value: 10
index: 2, value: 20
index: 3, value: 30
index: 4, value: 40
index: 5, value: 50
index: 6, value: 60
index: 7, value: 70
index: 8, value: 80
index: 9, value: 90
到此這篇關于C語言排序之 堆排序的文章就介紹到這了,更多相關C語言排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++報錯`Null Pointer Dereference`的解決方法
在軟件開發(fā)中,Null Pointer Dereference 是一種常見的錯誤,它發(fā)生在程序試圖訪問或操作一個空指針指向的內存位置時,這種情況通常會導致程序崩潰,給 debug 工作帶來很大困擾,今天,我們將探討如何解決 Null Pointer Dereference 報錯,需要的朋友可以參考下2024-07-07