用C 實現堆排序

2021-08-09 16:41:13 字數 784 閱讀 4510

極大堆(排序後從小到大):是具有以下性質的完全二叉樹,每個結點的值都大於等於左右結點的值

下面是使用極大堆的方式進行排序,使用極小堆的排序原理差不多。

#include

using namespace std;

void heapadjust(int a,int s,int n);

void heapsort(int a,int n);

void swap(int a,int i,int j);

int main();

heapsort(a,10);

for(int i=1;i<10;++i)

cout << a[i];

cout << endl;

system("pause");

return 0;}

void heapsort(int a,int n)

for(i=n-1;i>1;--i)

}void swap(int a,int i,int j)

void heapadjust(int a,int s,int n)

if(temp >= a[i])

break;

a[s]=a[i];

s=i;

}

a[s]=temp;

}

用C實現堆排序

堆排序 heapsort 是指利用堆積樹 堆 這種 資料結構所設計的一種 排序演算法,它是選擇排序的一種。可以利用 陣列的特點快速定位指定索引的元素。堆分為大根堆和小根堆,是 完全二叉樹。本次堆排序利用了小根堆堆頂記錄的 關鍵字最小這一特徵,使得在當前無序區中選取最小關鍵字的記錄變得簡單。用小根堆排...

用python實現堆排序

一 概念 將剩餘的堆繼續調整為最大堆,具體過程在第二塊有介紹,以遞迴實現 剩餘部分調整為最大堆後,再次將堆頂的最大數取出,再將剩餘部分調整為最大堆,這個過程持續到剩餘數只有乙個時結束 coding utf 8 author alex li import time,random defsift dow...

c 實現堆排序

include include include using namespace std 對比枝節點和左右子節點,將比較大的節點放置於枝節點 template int heapadjust t t,int i,int size if rightchild size t rightchild t max...