資料結構 堆排序

2021-07-09 02:13:57 字數 1131 閱讀 1051

1、堆排序的時間複雜度與歸併排序相同,o(nlogn)。堆排序的優勢在與他只需要固定數量的額外空間,

堆排序要比空間複雜性為o(n)的歸併排序稍微慢一些,但是比空間複雜性為o(1)的歸併排序要快。

2、對序列(26, 5, 77, 1, 61, 11, 59, 15, 48, 19)進行堆排序

過程:

/* * 調整最大堆(二叉堆)

*/void adjust(int list, int root, int n) else

}//向上調整,所以最後要處理root 中的值

list[child/2] = temp;

}void heapsort(int list)

//將root刪除(放在陣列的最後),調整最大堆

for(j = n-1; j>0; j--)

}int main() {

int list[max_size];

list[1] = 26;

list[2] = 5;

list[3] = 77;

list[4] = 1;

list[5] = 61;

list[6] = 11;

list[7] = 59;

list[8] = 15;

list[9] = 48;

list[10] = 19;

for(int i = 1; i

資料結構 堆排序 堆排序 Heap Sort

堆排序是一種選擇排序,其時間複雜度為o nlogn 堆的定義 n個元素的序列當且僅當滿足下列關係之一時,稱之為堆。情形1 ki k2i 且ki k2i 1 最小化堆或小頂堆 情形2 ki k2i 且ki k2i 1 最大化堆或大頂堆 其中i 1,2,n 2向下取整 若將和此序列對應的一維陣列 即以一...

資料結構 堆排序

include include void maxheapify int a,int length,int i void buildmaxheapify int a,int length void heapsort int a,int length void main void printf heap...

資料結構 堆排序

堆排序 heapsort 是指利用 堆這種資料 結構所設計 的一種排序演算法。堆是乙個近似完全二叉樹的結構,並同時滿足堆性質 即子結點的鍵值或索引總是小於 或者大於 它的父節點。pragma once include includetemplateclass heap heap const t a,...