国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

堆排序-大根堆

2023-01-12 15:59 作者:就叫大嘴吧  | 我要投稿


#include<stdio.h>
#include<stdlib.h>

using namespace std;

void swap(int &a, int &b){
	int temp = a;
	a = b;
	b = temp;
} 

// end:元素個數(shù) 
void HeapAdjust(int r[], int k, int end){
	int i = k; // 根節(jié)點 
	int j = 2 * k; // 左孩子節(jié)點 
	while(j <= end){
		// j < end:當前該節(jié)點 有右孩子
		// r[j] < r[j + 1]:左子樹值 < 右子樹值 
		if(j < end && r[j] < r[j + 1]){
			// j 指向 左右子樹值較大者:右子樹 
			j++; 
		}
		// 根結點值 < 孩子結點值 
		if(r[i] < r[j]){
			swap(r[i], r[j]);
		}
		// 向下進行調整 
		i = j;
		j = 2 * i;
	}
}

void HeapSort(int a[], int n){
	int k;
	// 建立 大根堆 
	for(k = n / 2; k >= 1; k--){
		HeapAdjust(a, k, n);
	}

	// 調整 (向下調整) 
	for(k = 1; k < n; k++){
		// 將 當前的堆頂 與 當前無序區(qū)的最后一個 進行交換 
		swap(a[1], a[n - k + 1]);
		
		// 1:需要調整的根結點
		// n - k:需要調整的元素個數(shù) 
		HeapAdjust(a, 1, n - k);
	}
} 

int main(){
	// a[0] 未使用 
	int a[7] = {0, 28, 25, 16, 36, 18, 32};
	
	for(int i = 1; i < 7; i++){
		printf("%d	", a[i]);
	}
	printf("\n");
	
	HeapSort(a, 6);
	
	for(int i = 1; i < 7; i++){
		printf("%d	", a[i]);
	}
	
	return 0;
}


堆排序-大根堆的評論 (共 條)

分享到微博請遵守國家法律
岐山县| 佛学| 和龙市| 江华| 莱州市| 文登市| 固镇县| 清水河县| 武陟县| 长兴县| 阿城市| 三都| 利川市| 温州市| 鄯善县| 新兴县| 常山县| 长子县| 东阳市| 刚察县| 商水县| 方山县| 汤原县| 平昌县| 赣榆县| 阳谷县| 西乡县| 灵宝市| 呼图壁县| 南阳市| 怀化市| 阿坝| 巨鹿县| 二手房| 镶黄旗| 石嘴山市| 开阳县| 瑞丽市| 雅安市| 彭州市| 疏勒县|