【我的创作纪念日】

机缘

大家好,我是圥忈ゼ, 2023 年 07 月 20 日,我撰写了第 1 篇技术博客:《我的编程未来规划》,也是由于我高考后的专业选择,和就业方向的选择,加上想立志成为一名专业 IT 作者,我结实了CSDN这个优秀,强大的平台,是CSDN给了我创作的平台,机会,和流量,让我一步步走向现在的128天了,在这段时间里,我已经获得了更大的成长。可能虽然日常忙碌但我还在坚持创作、可能初心还在但博客已良久未更新。最初成为创作者的初心,是为了记录自己的学习和成长历程,同时也希望能够通过分享自己的经验和知识,帮助更多的人。在这个过程中,我发现写作不仅可以加深自己对知识的理解,还可以锻炼自己的表达能力和思维能力。通过写作,我可以更加清晰地梳理自己的思路,并且将自己的想法传达给其他人。此外,写作还可以帮助我与其他志同道合的人交流和互动,扩展自己的人脉和视野。


收获

在创作的过程中,我获得了440位粉丝的关注,也得到了很多正向的反馈,如获得1,399次点赞、内容获得839次评论、获得838次收藏、28,404的阅读量等。这些反馈让我感到自己的努力得到了认可,也让我更加有动力继续创作。同时,我也认识了很多志同道合的领域同行和大佬比如:东离与糖宝江城开朗的豌豆,Aileen_0v0,等等,很多优秀的大佬我们一起交流和学习,互相鼓励和支持。


日常

当前创作已经成为了我生活的一部分。虽然日常工作和学习非常忙碌,但我仍然会抽出时间来写作。在有限的精力下,我会平衡创作和工作学习,合理安排时间,确保自己能够兼顾两者。


成就

1.过去写得最好的一段代码是一个实现数据结构和算法的代码数据结构 —— 堆的实现(顺序表),它高效地解决了一个时间复杂度很高的排序问题和在大量的数据中筛选前top-k最值的问题。这段代码不仅在性能上表现出色,而且在代码的可读性和可维护性方面也做得很好。代码如下:

一.头文件的实现 —— (Heap.h)

Heap.h
#pragma once


#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>


//小堆
typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a;
	int size;
	int capacity;
}HP;

//创建/销毁
void HeapInit(HP* php);
void HeapDestroy(HP* php);
//插入/删除
void HeapPush(HP* php, HPDataType x);
void HeapPop(HP* php);
//获取堆顶元素
HPDataType HeapTop(HP* php);
//判空/统计堆内元素个数
bool HeapEmpty(HP* php);
int HeapSize(HP* php);
//交换函数
void Swap(HPDataType* p1, HPDataType* p2);
//向上调整
void AdJustUp(HPDataType* a, int child);
//向下调整
void AdJustDown(HPDataType* a, int size, int parent);

二.源文件的实现 —— (Heap.c)

1.小堆的源文件
Heap.c
#include"Heap.h"


//小堆
//创建/销毁
void HeapInit(HP* php)
{
    
    
	assert(php);
	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}
void HeapDestroy(HP* php)
{
    
    
	assert(php);
	free(php->a);
	php->a = NULL;
	php->size = php->capacity = 0;
}

//交换函数
void Swap(HPDataType* p1, HPDataType* p2)
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

//向上调整函数
void AdJustUp(HPDataType* a, int child)
{
    
    
	int parent = (child - 1) / 2;
	while (child > 0)
	{
    
    
		if (a[child] < a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}
}
//插入/删除
void HeapPush(HP* php, HPDataType x)
{
    
    
	assert(php);
	if (php->size == php->capacity)//判断数组空间不够就扩容
	{
    
    
		int newcapacity = php->capacity == 0 ? 4 : php->capacity * 2;
		HPDataType* tmp = (HPDataType*)realloc(php->a, newcapacity * sizeof(HPDataType));
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			exit(-1);
		}
		php->a = tmp;
		php->capacity = newcapacity;
	}
	php->a[php->size] = x;
	php->size++;
	AdJustUp(php->a, php->size - 1);

}
//向下调整函数
void AdJustDown(HPDataType* a, int size, int parent)
{
    
    
	int child = parent * 2 + 1;
	while (child < size)
	{
    
    
		if (child+1 < size && a[child + 1] < a[child])
		{
    
    
			child++;
		}
		if (a[child] < a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}
void HeapPop(HP* php)
{
    
    
	assert(php);
	Swap(&php->a[0], &php->a[php->size - 1]);
	php->size--;
	AdJustDown(php->a, php->size, 0);
}
//获取堆顶元素
HPDataType HeapTop(HP* php)
{
    
    
	assert(php);
	assert(php->size > 0);
	return php->a[0];

}
//判空/统计堆内元素个数
bool HeapEmpty(HP* php)
{
    
    
	assert(php);
	return php->size == 0;
}
int HeapSize(HP* php)
{
    
    
	assert(php);
	assert(php->size > 0);
	return php->size;
}

2.大堆的源文件
Heap.h
#include"Heap.h"


//大堆
//创建/销毁
void HeapInit(HP* php)
{
    
    
	assert(php);
	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}
void HeapDestroy(HP* php)
{
    
    
	assert(php);
	free(php->a);
	php->a = NULL;
	php->size = php->capacity = 0;
}

//交换函数
void Swap(HPDataType* p1, HPDataType* p2)
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

//向上调整函数
void AdJustUp(HPDataType* a, int child)
{
    
    
	int parent = (child - 1) / 2;
	while (child > 0)
	{
    
    
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}
}
//插入/删除
void HeapPush(HP* php, HPDataType x)
{
    
    
	assert(php);
	if (php->size == php->capacity)//判断数组空间不够就扩容
	{
    
    
		int newcapacity = php->capacity == 0 ? 4 : php->capacity * 2;
		HPDataType* tmp = (HPDataType*)realloc(php->a, newcapacity * sizeof(HPDataType));
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			exit(-1);
		}
		php->a = tmp;
		php->capacity = newcapacity;
	}
	php->a[php->size] = x;
	php->size++;
	AdJustUp(php->a, php->size - 1);

}
//向下调整函数
void AdJustDown(HPDataType* a, int size, int parent)
{
    
    
	int child = parent * 2 + 1;
	while (child < size)
	{
    
    
		if (child+1 < size && a[child + 1] > a[child])
		{
    
    
			child++;
		}
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}
void HeapPop(HP* php)
{
    
    
	assert(php);
	Swap(&php->a[0], &php->a[php->size - 1]);
	php->size--;
	AdJustDown(php->a, php->size, 0);
}
//获取堆顶元素
HPDataType HeapTop(HP* php)
{
    
    
	assert(php);
	assert(php->size > 0);
	return php->a[0];

}
//判空/统计堆内元素个数
bool HeapEmpty(HP* php)
{
    
    
	assert(php);
	return php->size == 0;
}
int HeapSize(HP* php)
{
    
    
	assert(php);
	assert(php->size > 0);
	return php->size;
}

三.源文件的实现 —— (test.c)

test.c
#include"Heap.h"

//小堆
//int main()
//{
    
    
//	HP ph;
//	HeapInit(&ph);
//
//	int a[] = { 4,6,2,1,5,8,2,9};
//	for (int i = 0; i < (sizeof(a) / sizeof(int)); i++)
//	{
    
    
//		HeapPush(&ph, a[i]);//插入
//	}
//
//	//获取前k个最小值
//	/*int k = 3;
//	while (k--)
//	{
    
    
//		printf("%d\n", HeapTop(&ph));
//		HeapPop(&ph);
//	}*/
//	//小堆排序
//	while (!HeapEmpty(&ph))
//	{
    
    
//		printf("%d ", HeapTop(&ph));
//		HeapPop(&ph);
//	}
//
//	return 0;
//}

//大堆
//int main()
//{
    
    
//	HP ph;
//	HeapInit(&ph);
//	int a[] = { 4,6,2,1,5,8,2,9 };
//	for (int i = 0; i < (sizeof(a) / sizeof(int)); i++)
//	{
    
    
//		HeapPush(&ph, a[i]);
//	}
//	//前k个最大值
//	/*int k = 3;
//	while (k--)
//	{
    
    
//		printf("%d\n", HeapTop(&ph));
//		HeapPop(&ph);
//	}*/
//	//大堆排序
//	while(!HeapEmpty(&ph))
//	{
    
    
//		printf("%d ", HeapTop(&ph));
//		HeapPop(&ph);
//	}
//	return 0;
//}

//升序
//void HeapSort(int* a, int n)
//{
    
    
//	//建大堆/向上调整建堆/O(N*longN)
//	/*for (int i = 0; i < n; i++)
//	{
    
    
//		AdJustUp(a, i);
//	}*/
//	int end = n - 1;
//	//向下调整建堆
//	// O(N)
//	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
//	{
    
    
//		AdJustDown(a,n, i);
//	}
//	//O(N*longN)
//	while(end > 0)
//	{
    
    
//		Swap(&a[0], &a[end]);
//		AdJustDown(a, end, 0);
//		end--;
//	}
//}

//降序
//void HeapSort(int* a, int n)
//{
    
    
//	建小堆/向上调整建堆/O(N*longN)
//	/*for (int i = 0; i < n; i++)
//	{
    
    
//		AdJustUp(a, i);
//	}*/
//	int end = n - 1;
//	向下调整建堆
//	 O(N)
//	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
//	{
    
    
//		AdJustDown(a, n, i);
//	}
//	O(N*longN)
//	while (end > 0)
//	{
    
    
//		Swap(&a[0], &a[end]);
//		AdJustDown(a, end, 0);
//		end--;
//	}
//}
//int main()
//{
    
    
//	int a[] = { 4,6,2,1,5,8,2,9 };
//	int size = sizeof(a) / sizeof(int);
//	HeapSort(a, size);
//	for (int i = 0; i < size; i++)
//	{
    
    
//		printf("%d ", a[i]);
//	}
//	printf("\n");
//	return 0;
//}




//创造数据通过随机数
//void CreateNDate()
//{
    
    
//	int n = 100000;
//	srand(time(0));
//	const char* file = "date.txt";
//	FILE* fin = fopen(file, "w");
//	if (fin == NULL)
//	{
    
    
//		perror("fopen error");
//		return;
//	}
//	for (int i = 0; i < n; i++)
//	{
    
    
//		int x = (rand() + i) % 100000;
//		fprintf(fin, "%d\n", x);
//	}
//	fclose(fin);
//}
//
//void PrintTopk(const char* file, int k)
//{
    
    
//	FILE* fout = fopen(file, "r");
//	if (fout == NULL)
//	{
    
    
//		perror("fopen error");
//		return;
//	}
//	int* minheap = (int*)malloc(sizeof(int) * k);
//	if (minheap == NULL)
//	{
    
    
//		perror("malloc error");
//		return;
//	}
//	//创建一个k个值的小堆
//	for (int i = 0; i < k; i++)
//	{
    
    
//		fscanf(fout, "%d", &minheap[i]);
//		AdJustUp(minheap, i);
//	}
//	//让后从文件先读取k个数,形成小堆,让后再依次读取文件的数据和堆顶的数据比对,
//	// 如果比堆顶的数大就覆盖替换,让后向下调整,再形成小堆,
//	// 最后堆里剩下的就是这些数据里最大的前k个值
//	int x = 0;
//	while (fscanf(fout, "%d", &x) != EOF)
//	{
    
    
//		if (x < minheap[0])
//		{
    
    
//			minheap[0] = x;
//			AdJustDown(minheap, k, 0);
//		}
//	}
//
//	for (int i = 0; i < k; i++)
//	{
    
    
//		printf("%d ", minheap[i]);
//	}
//	printf("\n");
//
//	free(minheap);
//	fclose(fout);
//
//
//}


//创造数据通过随机数
void CreateNDate()
{
    
    
	int n = 100000;
	srand(time(0));
	const char* file = "date.txt";
	FILE* fin = fopen(file, "w");
	if (fin == NULL)
	{
    
    
		perror("fopen error");
		return;
	}
	for (int i = 0; i < n; i++)
	{
    
    
		int x = (rand() + i) % 100000;
		fprintf(fin, "%d\n", x);
	}
	fclose(fin);
}

void PrintTopk(const char* file, int k)
{
    
    
	FILE* fout = fopen(file, "r");
	if (fout == NULL)
	{
    
    
		perror("fopen error");
		return;
	}
	int* minheap = (int*)malloc(sizeof(int) * k);
	if (minheap == NULL)
	{
    
    
		perror("malloc error");
		return;
	}
	//创建一个k个值的大堆
	for (int i = 0; i < k; i++)
	{
    
    
		fscanf(fout, "%d", &minheap[i]);
		AdJustUp(minheap, i);
	}
	//让后从文件先读取k个数,形成大堆,让后再依次读取文件的数据和堆顶的数据比对,
	// 如果比堆顶的数小就覆盖替换,让后向下调整,再形成大堆,
	// 最后堆里剩下的就是这些数据里最小的前k个值
	int x = 0;
	while (fscanf(fout, "%d", &x) != EOF)
	{
    
    
		if (x < minheap[0])
		{
    
    
			minheap[0] = x;
			AdJustDown(minheap, k, 0);
		}
	}

	for (int i = 0; i < k; i++)
	{
    
    
		printf("%d ", minheap[i]);
	}
	printf("\n");

	free(minheap);
	fclose(fout);


}
int main()
{
    
    
	//CreateNDate();
	PrintTopk("date.txt", 5);
	return 0;
}

堆排序和top-k问题展示

展示图片来源于文章-数据结构 —— 堆的实现(顺序表)
在这里插入图片描述


憧憬

关于职业规划,我希望能够在技术领域不断深入学习和探索,成为一名专业的 IT 作者。同时,我也希望能够继续创作,分享更多有价值的内容,帮助更多的人。

创作规划方面,我希望能够更加系统地整理和总结自己的知识和经验,写出更加深入和专业的文章。同时,我也希望能够尝试不同的创作形式,如视频、直播等,与更多的人进行互动和交流。

以上就是我的纪念日博客,希望未来的日子里,我能够继续保持创作的热情,不断进步和成长。

Tips

  1. 您发布的文章将会展示至 里程碑专区 ,您也可以在 专区 内查看其他创作者的纪念日文章
  2. 优质的纪念文章将会获得神秘打赏哦

猜你喜欢

转载自blog.csdn.net/qq_73900397/article/details/134642349
今日推荐