如何用小堆打印数组的升序?

news/2024/7/5 5:28:09

堆的实现: 

#include <iostream >
using namespace std;
#include <assert.h>

typedef struct Heap
{
	int* array;
	int size;
	int capacity;
}HP;

void HeapInit(HP* hp)
{
	hp->array = nullptr;
	hp->capacity = hp->size = 0;
}

void HeapDestroy(HP* hp)
{
	assert(hp);
	free(hp->array);
	hp->array = nullptr;
	hp->capacity = hp->size = 0;
}

void swap(int* hp1, int* hp2)
{
	int tmp = *hp1;
	*hp1 = *hp2;
	*hp2 = tmp;
}

//小堆
void adjustUp(int* array, int child)
{
	int parent = (child - 1) / 2;

	while (child>0)
	{
		if (array[child] < array[parent])
		{
			swap(&(array[child]), &(array[parent]));
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

void HeapPush(HP* hp, int x)
{
	assert(hp);
	if (hp->size == hp->capacity)
	{
		//扩容
		int newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		int* newA =(int*) realloc(hp->array, sizeof(int) * newCapacity);
		if (newA==nullptr)
		{
			printf("realloc fail");
		}
		hp->capacity = newCapacity;
		hp->array = newA;
	}
	hp->array[hp->size] = x;
	hp->size++;

	adjustUp(hp->array,hp->size-1);
}

int HeapTop(HP* hp)
{
	assert(hp);
	assert(hp->size > 0);
	return hp->array[0];
}

bool HeapEmpty(HP* hp)
{
	assert(hp);
	return hp->size == 0;
}

int HeapSize(HP* hp)
{
	assert(hp);
	return hp->size;
}

void adjustDown(int* array, int parent, int size)
{
	//默认最小孩子为左孩子
	int child = parent * 2 + 1;

	//判断
	while (child < size)
	{
		//确保child为最小孩子
		if (child + 1 < size && array[child + 1] < array[child])
		{
			child = child + 1;
		}

		if (array[child] < array[parent])
		{
			swap(&(array[child]), &array[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void HeapPop(HP* hp)
{
	assert(hp);
	assert(hp->size > 0);
	swap(&(hp->array[0]), &(hp->array[hp->size - 1]));
	hp->size--;

	adjustDown(hp->array,0,hp->size);
}

 题目需要的代码:

int main()
{
	HP hp;
	HeapInit(&hp);
	int a[] = { 27,15,19,18,28,34,65,49,25,37 };
	for (int i = 0; i < sizeof(a) / sizeof(int); i++)
	{
		HeapPush(&hp, a[i]);
	}
	while (!HeapEmpty(&hp))
	{
		cout << HeapTop(&hp) << " " ;
		HeapPop(&hp);
	}
	HeapDestroy(&hp);
}


http://lihuaxi.xjx100.cn/news/1761441.html

相关文章

Linux | 磁盘文件与动静态库

目录 前言 一、了解磁盘 1、磁盘结构 2、磁盘划分 3、inode与文件名的关系 二、软链接与硬链接 1、如何创建软连接与硬链接文件 2、理解软连接 3、理解硬链接 三、动态库与静态库 1、静态库 &#xff08;1&#xff09;静态库的制作 &#xff08;2&#xff09;静态…

Day 52 单调栈 part01

Day 52 单调栈 part01 解题理解739496 2道题目 739. 每日温度 496. 下一个更大元素 I 解题理解 739 需要找的是当前位置右侧第一个大于它的下标&#xff0c;所以栈中存的是递增元素的下标&#xff0c;不递增的都pop()出来算距离。 class Solution:def dailyTemperatures(se…

2023最新【支付测试】

引言&#xff1a;如今&#xff0c;随着非现金支付手段的不断推广和应用&#xff0c;“非现金社会”正在形成。非现金支付已成为日常生活中不可或缺的伙伴。那么&#xff0c;对于互联网产品来说&#xff0c;支付也是涉及到公司收入的一个重大环节。对于我们测试人员&#xff0c;…

O - Happy Matt Friends

思路&#xff1a; &#xff08;1&#xff09;条件及问题&#xff1a;给定N个数&#xff0c;找到异或值大于等于M的总方案数&#xff1b; &#xff08;2&#xff09;分析&#xff1a; 可以dfs&#xff08;&#xff09;枚举&#xff0c;超时&#xff1b;考虑dp,dp[i][j]描述在…

shell 随机数

方法一&#xff1a;使用/dev/urandom方法二&#xff1a;使用date %s1.随机生成一串数字2.随机生成一串小写字母3.随机生成数字与字母的组合 方法三&#xff1a;使用openssl rand 方法一&#xff1a;使用/dev/urandom [rootlocalhost shell]# tr -dc "0-9" < /dev…

【uniapp】解决在H5谷歌浏览器下 u-input 标签 设置只读后,click事件不生效

【问题描述】 谷歌浏览器更新后&#xff0c;h5模式下原本的input外层view中的click事件不触发了?? 但是更换浏览器后就可以&#xff0c;打包app也是正常可以触发的&#xff0c;本来是没打算兼容h5&#xff0c;既然遇到了就记录一下~ 【解决办法】 使u–input里写上readonly&…

将json数据导入到ES集群——解决方案对比填坑日记

需求 将写好的json数据。导入到es集群 数据说明 文件JSON数据&#xff0c;一行一个JSON。 {"id":"d2716ae8fba4e026c4bd9445c3f49e2c","lang":"zh","title":"吉美旅馆","content":"吉美..."}…

[CISCN2019 华北赛区 Day2 Web1]Hack World1

提示 基于布尔的盲注使用python脚本跑 这里已经提示flag在flag表在flag字段 首先输入1 2都能有回显 每当这个时候第一想到的都应该是基于布尔的盲注是否能使用 尝试fuzz 通过fuzz大概知道后续思路 应为过滤的比较全面所以放弃联合查询 报错查询 预设置 使用基于布尔的盲注…