最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)堆排序示例

 更新時(shí)間:2021年08月26日 16:43:45   作者:雙魚(yú)211  
這篇文章主要介紹了C++實(shí)現(xiàn)堆排序示例,全文運(yùn)用大量代碼完成堆排序,需要了解的朋友可以參考一下這篇文章

堆的實(shí)現(xiàn)

Heap.h 堆的管理及接口

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

typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}Heap;

//堆的向下調(diào)整算法
void AdjustDown(HPDataType* a, int n, int root);
//堆的向上調(diào)整算法
void AdjustUp(HPDataType* a, int child);
//堆的初始化
void HeapInit(Heap* php, HPDataType* a,int n);
//堆的銷(xiāo)毀
void HeapDestroy(Heap* php);
//堆的插入
void HeapPush(Heap* php, HPDataType x);
//堆的刪除
void HeapPop(Heap* php);
//堆里的數(shù)據(jù)個(gè)數(shù)
int HeapSize(Heap* php);
//判斷堆是否為空
int HeapEmpty(Heap* php);
//取堆頂數(shù)據(jù)
HPDataType HeapTop(Heap* php);

Heap.c 堆各個(gè)接口功能的實(shí)現(xiàn)

• 堆的插入:將x插入下標(biāo)為size的位置,++size然后使用向上調(diào)整算法調(diào)整
• 堆的刪除(刪棧頂數(shù)據(jù)):將棧頂數(shù)據(jù)和下標(biāo)為size-1位置的數(shù)據(jù)交換,然后–size,使用向下調(diào)整算法調(diào)整

#include "Heap.h"

//堆向下調(diào)整算法
//建小堆
void AdjustDown(HPDataType* a, int n, int root)
{
	int parent = root;
	int child = parent * 2 + 1;
	//孩子超過(guò)數(shù)組下標(biāo)結(jié)束
	while (child < n)
	{
		//child始終左右孩子中小的那個(gè)
		if (a[child + 1] < a[child] && child + 1 <n)//防止沒(méi)有右孩子
		{
			++child;
		}
		//小的往上浮,大的往下沉
		if (a[child] < a[parent])
		{
			int tem = a[parent];
			a[parent] = a[child];
			a[child] = tem;
			parent = child;
			child = parent * 2 + 1;
		}
		//中途child>parent則已滿(mǎn)足小堆,直接break
		else
		{
			break;
		}
	}
}
//堆的向上調(diào)整算法
//建小堆
void AdjustUp(HPDataType* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
		{
			int tem = a[parent];
			a[parent] = a[child];
			a[child] = tem;
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
//堆的初始化
void HeapInit(Heap* php, HPDataType* a, int n)
{
	assert(php);
	assert(a);
	php->a = (HPDataType*)malloc(n * sizeof(HPDataType));
	if (php->a == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	for (int i = 0; i < n; i++)
	{
		php->a[i] = a[i];
	}
	//建堆
	for (int i = (n - 2) / 2; i >= 0; --i)
	{
		AdjustDown(php->a, n, i);
	}
	php->capacity = n;
	php->size = n;
}
//堆的銷(xiāo)毀
void HeapDestroy(Heap* php)
{
	assert(php);
	free(php->a);
	php->a = NULL;
	php->capacity = 0;
	php->size = 0;
}
//堆的插入
void HeapPush(Heap* php, HPDataType x)
{
	assert(php);
	if (php->size == php->capacity)
	{
		HPDataType* tem = (HPDataType*)realloc(php->a,php->capacity * 2 * sizeof(HPDataType));
		if (tem == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		php->a = tem;
		php->capacity *= 2;
	}
	php->a[php->size] = x;
	++php->size;
	AdjustUp(php->a,php->size - 1);
}
//堆的刪除
void HeapPop(Heap* php)
{
	assert(php);
	assert(php->size > 0);
	HPDataType tem = php->a[php->size - 1];
	php->a[php->size - 1] = php->a[0];
	php->a[0] = tem;
	--php->size;
	AdjustDown(php->a, php->size, 0);
}
//堆里的數(shù)據(jù)個(gè)數(shù)
int HeapSize(Heap* php)
{
	assert(php);
	return php->size;
}
//判斷堆是否為空
//為空返回1,不為空返回0
int HeapEmpty(Heap* php)
{
	assert(php);
	return php->size == 0 ? 1 : 0;
}
//取堆頂數(shù)據(jù)
HPDataType HeapTop(Heap* php)
{
	assert(php);
	assert(php->size > 0);
	return php->a[0];
}

test.c測(cè)試

#include "Heap.h"

void TestHeap()
{
	int arr[] = { 27, 28, 65, 25, 15, 34, 19, 49, 18, 37 };
	Heap hp;
	HeapInit(&hp, arr, sizeof(arr)/sizeof(int));
	while (!HeapEmpty(&hp))
	{
		printf("%d ", HeapTop(&hp));
		HeapPop(&hp);

	}
	printf("\n");
	HeapDestroy(&hp);
}
int main()
{
	TestHeap();
	return 0;
}

以上就是C++實(shí)現(xiàn)堆排序示例的詳細(xì)內(nèi)容,更多關(guān)于C++實(shí)現(xiàn)堆排序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 用代碼和UML圖化解設(shè)計(jì)模式之橋接模式的深入分析

    用代碼和UML圖化解設(shè)計(jì)模式之橋接模式的深入分析

    本篇文章是對(duì)橋接模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 深入理解C++中的文件操作

    深入理解C++中的文件操作

    這篇文章主要給大家深入的介紹了C++中的文件操作,文件的操作對(duì)每個(gè)程序員來(lái)說(shuō)都是很重要的,本文的介紹的很詳細(xì),有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • C++短路求值(邏輯與、邏輯或)實(shí)例

    C++短路求值(邏輯與、邏輯或)實(shí)例

    這篇文章主要介紹了C++短路求值(邏輯與、邏輯或)實(shí)例,以實(shí)例形式講述了邏輯或的短路與邏輯與的短路及相應(yīng)的應(yīng)用實(shí)例,需要的朋友可以參考下
    2014-10-10
  • C++中const與#define的利弊分析

    C++中const與#define的利弊分析

    C++中不但可以用define定義常量還可以用const定義常量,下面這篇文章主要給大家分析介紹了關(guān)于C++中const與#define的利弊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2018-05-05
  • C++中引用處理的基本方法

    C++中引用處理的基本方法

    引用不是新定義了一個(gè)變量,而是給已經(jīng)存在的變量取了一個(gè)別名,編譯器不會(huì)為引用變量開(kāi)辟內(nèi)存空間,他和他引用的變量共用一塊內(nèi)存空間,下面這篇文章主要給大家介紹了關(guān)于C++中引用處理的基本方法,需要的朋友可以參考下
    2022-12-12
  • C語(yǔ)言對(duì)冒泡排序進(jìn)行升級(jí)介紹

    C語(yǔ)言對(duì)冒泡排序進(jìn)行升級(jí)介紹

    大家好,本篇文章主要講的是C語(yǔ)言對(duì)冒泡排序進(jìn)行升級(jí)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • 基于c++11的event-driven library的理解

    基于c++11的event-driven library的理解

    這篇文章主要介紹了基于c++11的event-driven library的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 詳解C語(yǔ)言學(xué)習(xí)記錄之指針

    詳解C語(yǔ)言學(xué)習(xí)記錄之指針

    關(guān)于指針,其是C語(yǔ)言的重點(diǎn),C語(yǔ)言學(xué)的好壞,其實(shí)就是指針學(xué)的好壞。其實(shí)指針并不復(fù)雜,學(xué)習(xí)指針,要正確的理解指針,本片文章能給就來(lái)學(xué)習(xí)一下
    2021-11-11
  • C++ 關(guān)于MFC多線程編程的注意事項(xiàng)

    C++ 關(guān)于MFC多線程編程的注意事項(xiàng)

    這篇文章主要介紹了C++ 關(guān)于MFC多線程編程的注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • 深入解讀C++ 內(nèi)聯(lián)函數(shù)inline|nullptr

    深入解讀C++ 內(nèi)聯(lián)函數(shù)inline|nullptr

    內(nèi)聯(lián)函數(shù):用** inline 修飾的函數(shù)叫做內(nèi)聯(lián)函數(shù),編譯時(shí)C++編譯器會(huì)在調(diào)用的地方展開(kāi)內(nèi)聯(lián)函數(shù)**,這樣調(diào)用內(nèi)聯(lián)函數(shù)就需要?jiǎng)?chuàng)建棧楨,就提高效率了,這篇文章給大家介紹C++ 內(nèi)聯(lián)函數(shù)inline|nullptr的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2024-07-07

最新評(píng)論

吉木萨尔县| 容城县| 曲沃县| 福海县| 泊头市| 金湖县| 桓台县| 海阳市| 特克斯县| 宿迁市| 建湖县| 望奎县| 沂源县| 淄博市| 布拖县| 团风县| 汾西县| 攀枝花市| 济宁市| 皋兰县| 交城县| 通许县| 靖远县| 通榆县| 大竹县| 绩溪县| 德江县| 南溪县| 多伦县| 盐城市| 介休市| 左权县| 阳高县| 门源| 平泉县| 昌黎县| 启东市| 壶关县| 绥棱县| 区。| 惠州市|