。存个码。

这篇博客主要介绍了C++实现堆排序的过程,包括最大堆和最小堆的建立、插入、删除以及排序。作者通过详细代码展示了如何创建堆、调整堆以及进行堆排序,同时提供了快速幂运算和对数计算的辅助函数。堆排序算法的时间复杂度为O(nlogn),适合处理大数据集。
摘要由CSDN通过智能技术生成

没啥,真的只是MengXin存个码,DaLao勿入

刚学了堆,练一下,,

//#pragma GCC optimize(2)
#define _CRT_SECURE_NO_WARNINGS
#define NDEBUG
#include<bits/stdc++.h>
#define MAX 0x3f3f3f3f
#define MIN 0xc0c0c0c1
#define PI 3.141592653589793238
#define max(x,y) (x>y ? x:y)
#define min(x,y) (x<y ? x:y)
#define forn(i,n) for(int i = 0; i < int(n); i++)
#define endl '\n'
using namespace std;
class HEAP {
private:
	typedef int HeapType;
	typedef struct heap {
		HeapType* inf;
		int size;
		int capacity;
	} heap;
public:
	
	void HeapCreat(HeapType* arr, int number_num) {
		Heap = Heap_Creat(arr, number_num);
	}
	void Heap_Push(HeapType val, bool ins) {
		assert(Heap);
		Check_Capacity(Heap);
		Heap->inf[Heap->size++] = val;
		ShiftUp(Heap->inf, Heap->size - 1, ins);
	}
	void HeapDestory() {
		Heap_Destory(Heap);
	}
	void HeapSort(HeapType* arr, int length, bool ins) {
		if (!Heap) {
			Heap = Heap_Creat(arr, length);
		}
		Heap_Sort(Heap, ins);
		memcpy(arr, Heap->inf, sizeof(HeapType) * length);
	}
private:
	heap* Heap = nullptr;
	void ShiftDown(HeapType* tree, int val_num, int begin, bool ins) {
		int child = 2 * begin + 1;
		while (child < val_num) {
			bool t = ins == 1 ? (tree[child + 1] < tree[child]) : (tree[child + 1] > tree[child]);
			if (child + 1 < val_num && t) {
				child++;
			}
			bool x = ins == 1 ? (tree[child] < tree[begin]) : (tree[child] > tree[begin]);
			if (x) {
				swap(&tree[child], &tree[begin]);
				begin = child;
				child = 2 * begin + 1;
			}
			else break;
		}
	}
	void ShiftUp(HeapType* tree, int begin, bool ins) {
		int parent = (begin - 1) / 2;
		while (begin > 0) {
			bool t = ins == 1 ? (tree[begin] < tree[parent]) : (tree[begin] > tree[parent]);
			if (t) {
				swap(&tree[begin], &tree[parent]);
				begin = parent;
				parent = (begin - 1) / 2;
			}
			else break;
		}
	}
	void Heap_Destory(heap* hp) {
		assert(hp);
		free(hp->inf);
		hp->inf = nullptr;
		hp->capacity = hp->size = 0;
	}
	bool ifHeapEmpty(heap* hp) {
		if (hp == nullptr || hp->size == 0) return true;
		else return false;
	}
	void Heap_Sort(heap* hp, bool ins) {
		assert(hp);
		for (int i = (hp->size - 2) / 2; i >= 0; i--)
		{
			ShiftDown(hp->inf, hp->size, i, ins);
		}
		int end = hp->size - 1;
		while (end > 0)
		{
			swap(&hp->inf[0], &hp->inf[end]);
			ShiftDown(hp->inf, end, 0, ins);
			end--;
		}
	}
	heap* Heap_Creat(HeapType* target_arr, int val_num) {
		heap* hp = (heap*)malloc(sizeof(heap) * 1);
		Heap_Initialize(hp);
		assert(hp);
		if (val_num > 0) {
			hp->inf = (HeapType*)malloc(sizeof(HeapType) * val_num);
			memcpy(hp->inf, target_arr, sizeof(HeapType) * val_num);
		}
		hp->capacity = hp->size = val_num;
		for (int i = (val_num - 2) / 2; i >= 0; i--) {
			ShiftDown(hp->inf, hp->size, i, 0);
		}
		return hp;
	}
	void Heap_Initialize(heap* hp) {
		hp->inf = nullptr;
		hp->capacity = hp->size = 0;
	}
	void Check_Capacity(heap* hp) {
		if (hp->capacity == hp->size) {
			int NewInc = hp->capacity == 0 ? 1 : 2 * hp->capacity;
			hp->inf = (HeapType*)realloc(hp->inf, sizeof(HeapType) * NewInc);
			hp->capacity = NewInc;
		}
	}
	void swap(HeapType* a, HeapType* b) {
		HeapType t = *a;
		*a = *b;
		*b = t;
	}
	int quickX(int val, int x) {
		int result = 1;
		while (x > 0) {
			if (x & 1)
				result = result * val;
			x >>= 1;
			val = val * val;
		}
		return result;
	}
	int log_x_T(int bottom, int target) {
		return (int)(log(target) / log(bottom));
	}
	void Heap_Pop(heap* hp) {
		if (hp->size > 0) {
			swap(&hp->inf[0], &hp->inf[hp->size - 1]);
			hp->size--;
			ShiftDown(hp->inf, hp->size, 0, 0);
		}
	}
};

typedef long long ll;
typedef unsigned long long ull;
typedef int QUICKPOW;
typedef ll READ;
typedef int WRITE;
inline READ digit_read();
inline string string_read();
inline double double_read();
void digit_write(WRITE x);
QUICKPOW quick_pow(QUICKPOW bottom, QUICKPOW power);
void quicksort(int number[], int l, int r, bool ins);//ins==0,升序,ins==1,降序
bool isCoprime(int x, int y);
inline bool ifinit(int nvert, double* vertx, double* verty, double testx, double testy);//点数目,定点坐标,测试点坐标

int main()
{
	HEAP Heap;
	int* num = nullptr;
	int i = 0;
	for (;; i++) {
		int n;
		if (scanf("%d", &n) == 1) {
			num = (int*)realloc(num, sizeof(int) * (i + 1));
			num[i] = n;
		}
		else break;
	}
	Heap.HeapSort(num, i, 0);
	for (int j = 0; j < i; j++) {
		printf("%d ", num[j]);
	}
	return 0;
}
inline READ digit_read() {
	char ch = getchar();
	READ f = 1, x = 0;
	while (ch > '9' || ch < '0') {
		if (ch == '-') f *= -1;
		ch = getchar();
	}
	while (ch <= '9' && ch >= '0') {
		x = x * 10 + (int)(ch - '0');
		ch = getchar();
	}
	return x * f;
}
inline string string_read() {
	string str;
	char s = getchar();
	while (s == ' ' || s == '\n' || s == '\r') {
		s = getchar();
	}
	while (s != ' ' && s != '\n' && s != '\r') {
		str += s;
		s = getchar();
	}
	return str;
}
inline double double_read() {
	long long s = 0, w = 1, k = 0, m = 0;
	bool n = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while ((ch >= '0' && ch <= '9') || ch == '.') {
		if (ch == '.')
			n = 1;
		else if (n == 0)
			s = s * 10 + ch - '0';
		else k = k * 10 + ch - '0', m++;
		ch = getchar();
	}
	return (pow(0.1, m) * k + s) * w;
}

void digit_write(WRITE x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) digit_write(x / 10);
	putchar(x % 10 + '0');
}
QUICKPOW quick_pow(QUICKPOW bottom, QUICKPOW power) {
	QUICKPOW right = 1;
	while (power) {
		if (power & 1)
			right = right * bottom;
		power >>= 1;
		bottom = bottom * bottom;
	}
	return right;
}
void quicksort(int number[], int l, int r, bool ins) {
	if (l < r) {
		int p = l;
		int index = p + 1;
		for (int i = index; i <= r; i++) {
			bool t = ins == 0 ? number[i] < number[p] : number[i] > number[p];
			if (t) {
				int t = number[index];
				number[index] = number[i];
				number[i] = t;
				index++;
			}
		}
		int t = number[p];
		number[p] = number[index - 1];
		number[index - 1] = t;
		p = index - 1;
		quicksort(number, l, p - 1, ins);
		quicksort(number, p + 1, r, ins);
	}
}
bool isCoprime(int x, int y)
{
	if (x == 1 && y == 1) return true;
	else if (x <= 0 || y <= 0 || x == y) return false;
	else if (x == 1 || y == 1) return true;
	else {
		int tmp = x % y;
		while (tmp) {
			x = y;
			y = tmp;
			tmp = x % y;
		}
		if (y == 1) return true;
		else return false;
	}
}
inline bool ifinit(int nvert, double* vertx, double* verty, double testx, double testy) {
	int i, j;
	bool c = false;
	for (i = 0, j = nvert - 1; i < nvert; j = i++) {
		if (((verty[i] > testy) != (verty[j] > testy)) &&
			((testx - vertx[i]) * (verty[j] - verty[i]) < (vertx[j] - vertx[i]) * (testy - verty[i])))
			c = !c;
	}
	return c;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿白|

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值