保定网站建设求职简历,产品广告策划方案,企业网站设计风格,网络营销方式变化的逻辑NO.1 概念
堆排序是利用堆这种数据结构而设计的一种排序算法#xff0c;堆排序是一种选择排序#xff0c;时间复杂度均为O(nlogn)#xff0c;是不稳定排序。 堆是完全二叉树 NO.2 如何手写一个堆
1. 插入一个数 heap[size] x; up(size);
2. 求集合当中的最小值 heap[1…NO.1 概念
堆排序是利用堆这种数据结构而设计的一种排序算法堆排序是一种选择排序时间复杂度均为O(nlogn)是不稳定排序。 堆是完全二叉树 NO.2 如何手写一个堆
1. 插入一个数 heap[size] x; up(size);
2. 求集合当中的最小值 heap[1];
3.删除最小值 heap[1] heap[size];size--;down(1);
4.删除任意一个元素 heap[k] heap[size];size--;down[k];up[k];
5.修改任意一个元素 heap[k] x;down[k];up[k]; NO.3 例题堆排序
来源838. 堆排序 - AcWing题库
题目 输入一个长度为 n 的整数数列从小到大输出前 m 小的数。 输入格式 第一行包含整数 n 和 m。 第二行包含 n 个整数表示整数数列。 输出格式 共一行包含 m 个整数表示整数数列中前 m 小的数。 数据范围 1≤m≤n≤10^5 1≤数列中元素≤10^9 输入样例 5 3
4 5 1 3 2输出样例 1 2 3 代码
#includeiostream
#includecstdio
#includestring
#includecstring
#includestring.h
#includealgorithm
#includecmath
#includevector
using namespace std;
const int N 1e5 10;
int a[N],size1;
void down(int u){int t u;if(2*u size1 a[u*2] a[t]) t u*2;if(2*u1 size1 a[u*21] a[t]) t u*21; if(u ! t){swap(a[u],a[t]);down(t);}
}
int main(){int n,m;cin n m;size1 n;for(int i 1;i n;i )cin a[i];for(int i n/2;i 0;i --)down(i);while(m --){printf(%d ,a[1]);a[1] a[size1];size1 --;down(1);}return 0;
}