当前位置: 首页 > news >正文

【数据结构】用堆实现排序

1.升序------建大堆

堆的时间复杂度N*logN

#include<stdio.h>void Swap(int* p1, int* p2)
{int x = *p1;*p1 = *p2;*p2 = x;
}void AdjustUp(int* a, int child)
{int parent = (child - 1) / 2;while (child > 0){if (a[parent] < a[child]){Swap(&a[parent], &a[child]);child = parent;parent = (child - 1) / 2;}elsebreak;}}void AdjustDown(int* a, int n, int parent)
{int child = parent * 2 + 1;while (child < n){//筛选子节点中较大的数if (child + 1 < n && 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 HeapSort(int* a, int n)
{//建堆,向上调整建堆for (int i = 1;i < n;i++){AdjustUp(a, i);}//自己进行调整int end = n - 1;while (end>0){Swap(&a[end], &a[0]);AdjustDown(a, end,0);--end;}}int main()
{int a[10] = { 23,2,45,3,67,55,41,32,12,48 };HeapSort(&a, 10);for (int i = 0;i < 10;i++){printf("%d ", a[i]);}return 0;
}

2.降序------建小堆

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<time.h>void Swap(int* p1, int* p2)
{int x = *p1;*p1 = *p2;*p2 = x;
}void AdjustDown(int* a, int n, int parent)
{int child = parent * 2 + 1;while (child < n){//筛选子节点中较小的数if (child + 1 < n && 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 PrintTopk(const char*file, int k)
{//1.建堆——用file中前k个元素建小堆int* topk = (int*)malloc(sizeof(int) * k);assert(topk);FILE* fout = fopen(file, "r");if (fout == NULL){perror("fopen fail");return;}//读出前k个数据建小堆for (int i = 0;i < k;i++){fscanf(fout, "%d", &topk[i]);}for (int i = (k -1-1) / 2;i >= 0;i--)//k-1是最后一个数据的下标,在-1除以2是找到它的父节点{AdjustDown(topk, k, i);}//将剩余n-k个元素依次与堆顶元素进行交换,不满则替换int val = 0;int ret = fscanf(fout, "%d", &val);while (ret != EOF){if (val > topk[0]){topk[0] = val;AdjustDown(topk, k, 0);}ret = fscanf(fout, "%d", &val);}for (int i = 0;i < k;i++){printf("%d ", topk[i]);}printf("\n");free(topk);fclose(fout);
}void TestTopy()
{int n = 10000;srand(time(0));const char* file = "data.txt";FILE* fin = fopen(file, "w");if (fin == NULL){perror("fopen fail");return;}for (size_t i = 0;i < n;i++){int x = rand() % 10000;fprintf(fin, "%d\n", x);}fclose(fin);PrintTopk(file, 10);
}int main()
{TestTopy();return 0;
}

http://www.lryc.cn/news/604249.html

相关文章:

  • Typecho handsome新增评论区QQ,抖音,b站等表情包
  • python基础:request请求Cookie保持登录状态
  • 关于算法的一些思考
  • PyCharm插件开发与定制指南:打造个性化开发环境
  • Vulnhub napping-1.0.1靶机渗透攻略详解
  • ITIL 4 高速IT:解耦架构——构建快速迭代的技术基座
  • JDK17 新特性跟学梳理
  • 深入解析Java元注解与运行时处理
  • [leetcode] 组合总和
  • C++多态:面向对象编程的灵魂之
  • DeepCompare文件深度对比软件的差异内容提取与保存功能深度解析
  • ESP8266 AT 固件
  • 破解企业无公网 IP 难题:可行路径与实现方法?
  • 系统学习算法:专题十五 哈希表
  • 网络安全第15集
  • docker docker、swarm 全流程执行
  • vue3插槽详解
  • Linux 线程概念与控制
  • C#_ArrayList动态数组
  • 3D打印喷头的基本结构
  • [css]旋转流光效果
  • 机械臂抓取的无模型碰撞检测代码
  • Export useForm doesn‘t exist in target module
  • 前端手写贴
  • zoho crm为什么xx是deal的关联对象但是调用函数时报错说不是关联对象
  • Docker初学者需要了解的几个知识点(三):Docker引擎与Docker Desktop
  • BERT和GPT和ELMO核心对比
  • Redis 键值对操作详解:Python 实现指南
  • 字符串函数安全解析成执行函数
  • 解密数据结构之二叉树