当前位置: 首页 > 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.dtcms.com/a/303960.html

相关文章:

  • vue3+vite 使用liveplayer加载视频
  • MySQL MVCC:并发神器背后的原理解析
  • 网工知识——OSPF摘要知识
  • [工具类] 分片上传下载,MD5校验
  • echarts饼图
  • 封装$.ajax
  • 一个人开发一个App(数据库)
  • OpenAI Python API 完全指南:从入门到实战
  • 【学习笔记】Lean4 定理证明 ing
  • 7.29错题(zz)史纲 7章 建立新中国
  • Scala实用编程(附电子书资料)
  • Node.js 内置模块
  • AR辅助前端设计:虚实融合场景下的设备维修指引界面开发实践
  • 学习Scala语言的最佳实践有哪些?
  • GCC、glibc、GNU C(gnuc)的关系
  • SkSurface---像素的容器:表面
  • PowerShell脚本自动卸载SQL Server 2025和 SSMS
  • 零基础-动手学深度学习-7.7 稠密连接网络(DenseNet)
  • 景区负氧离子环境监测系统云平台方案
  • 论文阅读:2024 arxiv AutoDefense: Multi-Agent LLM Defense against Jailbreak Attacks
  • 【OpenAI】ChatGPT辅助编码:Spring Boot + Copilot自动生成业务逻辑
  • 【MySQL】从连接数据库开始:JDBC 编程入门指南
  • Java优雅使用Spring Boot+MQTT推送与订阅
  • vue请求golang后端CORS跨域问题深度踩坑
  • 【STM32】FreeRTOS 任务消息队列 和 中断消息队列的区别(六)
  • 14 - 大语言模型 — 抽取式问答系统 “成长记”:靠 BERT 学本事,从文本里精准 “揪” 答案的全过程(呆瓜版-1号)
  • “非参数化”大语言模型与RAG的关系?
  • 云原生MySQL Operator开发实战(五):扩展与生态系统集成
  • python使用ffmpeg录制rtmp/m3u8推流视频并按ctrl+c实现优雅退出
  • DateTime::ToString 日期时间文本格式化深度解析(C++)