堆实现和排序

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
typedef int HeapDatatype;
struct Heap
{
	HeapDatatype* arr;
	int size;
	int capacity;
};
typedef struct Heap HP;

void HPInit(HP* php);
void HPInitArray(HP* php,HeapDatatype* arr, int size);
void HPDestroy(HP* php);
void HPPush(HP* php, HeapDatatype x);
HeapDatatype HPTop(HP* php);
void HPPop(HP* php);
bool HPEmpty(HP* php);
void AdjustUp(HeapDatatype* arr, int child);
void AdjustDown(HeapDatatype* arr, int size, int parent);
void Swap(HeapDatatype* x, HeapDatatype* y);
 
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"
void HPInit(HP* php)
{
	assert(php);
	php->arr = NULL;
	php->size = 0;
	php->capacity = 0;
}
void HPInitArray(HP* php, HeapDatatype* arr, int size)
{
	assert(php);
	php->arr = (HeapDatatype*)malloc(sizeof(HeapDatatype) * size);
	if ( php->arr==NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	memcpy(php->arr, arr, sizeof(HeapDatatype) * size);
	php->capacity = php->size = size;
	//不好用的比较少
	//for (int i = 1; i < size; i++)
	//{
	//	AdjustUp(php->arr, i);
	//}


	//向下调整建堆
	for (int  i = (php->size-1-1)/2; i >= 0; i--)
	{
		AdjustDown(php->arr, php->size, i);
	}
}
void HPDestroy(HP* php)
{
	assert(php);
	free(php->arr);
	php->arr = NULL;
	php->size = 0;
	php->capacity = 0;
	
	
}
void Swap(HeapDatatype* x, HeapDatatype* y)
{
	HeapDatatype temp;
	 temp= *x;
	 *x = *y;
	 *y = temp;
}
void AdjustUp(HeapDatatype* arr, int child)
{
	int parent = (child - 1) / 2;
	while (child>0)
	{
		if (arr[child] > arr[parent])
		{
			Swap(&arr[parent], &arr[child]);
			child = parent;
			parent = (parent - 1) / 2;
		}
		else
		{
			break;
		}
		
	
	}
}
void HPPush(HP* php, HeapDatatype x)
{
	assert(php);
	size_t newcapacity;
	if (php->size == php->capacity)
	{
		if (php->capacity == 0)
		{
			newcapacity = 4;
		}
		else
		{
			newcapacity = php->capacity * 2;
		}
		HeapDatatype* temp = realloc(php->arr, sizeof(HeapDatatype) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		php->arr = temp;
		php->capacity = newcapacity;

	}
	php->arr[php->size] = x;
	php->size++;

	AdjustUp(php->arr, php->size-1);

}
HeapDatatype HPTop(HP* php)
{
	assert(php);
	return php->arr[0];
}
void AdjustDown(HeapDatatype* arr, int size, int parent)
{
	int child = parent * 2 + 1;
	while (child < size)
	{
		if (child+1<size && arr[child + 1] <arr[child])
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = child * 2 + 1;
		}
		else
		{
			break;
		}
	}
	
}

void HPPop(HP* php)
{
	assert(php);
	assert(php->size>0);

	Swap(&php->arr[0], &php->arr[php->size - 1]);

	php->size--;
	AdjustDown(php->arr, php->size, 0);
}
bool HPEmpty(HP* php)
{
	assert(php);
	return php->size == 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

void HeapSort(int* a, int n)
{


	for (int i = (n-1-1)/2; i >=0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;

	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}
}
int main()
{
	int arr[] = { 3,6,1,5,8,9,2,7,4,0 };
	HeapSort(arr, sizeof(arr) / sizeof(int));

}
//int main()
//{
//	int arr[] = { 46, 23 ,26, 24 ,10 };
//	HP hp;
//	HPInit(&hp);
//	for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
//	{
//		HPPush(&hp, arr[i]);
//	}
//	/*printf("%d\n", HPTop(&hp));
//	HPPop(&hp);
//	printf("%d\n", HPTop(&hp));*/
//
//	while (!HPEmpty(&hp))
//	{
//		printf("%d\n", HPTop(&hp));
//		HPPop(&hp); 
//	}
//
//
//	HPDestroy(&hp);
//	return 0;
//}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值