C++實(shí)現(xià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);
//堆的銷毀
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;
//孩子超過數(shù)組下標(biāo)結(jié)束
while (child < n)
{
//child始終左右孩子中小的那個(gè)
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ù)個(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)文章
C++實(shí)現(xiàn)LeetCode(41.首個(gè)缺失的正數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(41.首個(gè)缺失的正數(shù)),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)并優(yōu)化異常系統(tǒng)
異常處理是C++的一項(xiàng)語(yǔ)言機(jī)制,用于在程序中處理異常事件,下面這篇文章主要給大家介紹了關(guān)于C++中異常的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開始了)[四]
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開始了)[四]...2007-02-02
C++實(shí)現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助2023-03-03

