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

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

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

堆的實現(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);
//堆的銷毀
void HeapDestroy(Heap* php);
//堆的插入
void HeapPush(Heap* php, HPDataType x);
//堆的刪除
void HeapPop(Heap* php);
//堆里的數(shù)據(jù)個數(shù)
int HeapSize(Heap* php);
//判斷堆是否為空
int HeapEmpty(Heap* php);
//取堆頂數(shù)據(jù)
HPDataType HeapTop(Heap* php);

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

• 堆的插入:將x插入下標為size的位置,++size然后使用向上調(diào)整算法調(diào)整
• 堆的刪除(刪棧頂數(shù)據(jù)):將棧頂數(shù)據(jù)和下標為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;
	//孩子超過數(shù)組下標結(jié)束
	while (child < n)
	{
		//child始終左右孩子中小的那個
		if (a[child + 1] < a[child] && child + 1 <n)//防止沒有右孩子
		{
			++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則已滿足小堆,直接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;
}
//堆的銷毀
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ù)個數(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測試

#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++實現(xiàn)堆排序示例的詳細內(nèi)容,更多關(guān)于C++實現(xiàn)堆排序的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

    C語言對冒泡排序進行升級介紹

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

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

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

    詳解C語言學習記錄之指針

    關(guān)于指針,其是C語言的重點,C語言學的好壞,其實就是指針學的好壞。其實指針并不復雜,學習指針,要正確的理解指針,本片文章能給就來學習一下
    2021-11-11
  • C++ 關(guān)于MFC多線程編程的注意事項

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

    這篇文章主要介紹了C++ 關(guān)于MFC多線程編程的注意事項的相關(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ù),編譯時C++編譯器會在調(diào)用的地方展開內(nèi)聯(lián)函數(shù)**,這樣調(diào)用內(nèi)聯(lián)函數(shù)就需要創(chuàng)建棧楨,就提高效率了,這篇文章給大家介紹C++ 內(nèi)聯(lián)函數(shù)inline|nullptr的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2024-07-07

最新評論

中卫市| 连南| 堆龙德庆县| 沐川县| 腾冲县| 大方县| 荣成市| 普兰县| 石首市| 公主岭市| 白城市| 台南县| 阳山县| 淮南市| 河北区| 福泉市| 永泰县| 曲麻莱县| 阳谷县| 绥芬河市| 且末县| 灵璧县| 黄大仙区| 孝感市| 启东市| 韶山市| 互助| 内乡县| 吉隆县| 兴和县| 全南县| 宜良县| 临西县| 洪泽县| 高安市| 南部县| 康马县| 镇坪县| 五家渠市| 二手房| 太仆寺旗|