【kuandui】二叉堆
来源:互联网 发布:网络基础知识ppt 编辑:程序博客网 时间:2024/06/05 02:10
#include<iostream>
#include<cstdio>
using namespace std;
int n,k;
int num[1000000];
void heapup(int m)
{
int tmp=num[m];
int j=(m-1)/2;
while(j>=0&&m!=0)
{
if(num[j]<=tmp)
break;
num[m]=num[j];
m=j;
j=(m-1)/2;
}
num[m]=tmp;
}
void heapadd(int n,int shu)
{
num[n]=shu;
heapup(n);
}
//=========以上是插入==========//
void heapdown(int m,int lon)
{
int tmp=num[m];
int j=2*m+1;
while(j<lon)
{
if(j+1<lon&&num[j+1]<num[j])
++j;
if(num[j]>=tmp)
break;
num[m]=num[j];
m=j;
j=2*m+1;
}
num[m]=tmp;
}
//==========以上是删除==========//
void buildheap(int n)
{
for(int i=n/2-1;i>=0;--i)
heapdown(i,n);
}
void heapsort()
{
buildheap(n);
for(int i=n-1;i>=1;--i)
{
swap(num[0],num[i]);
heapdown(0,i-1);
}
}
//==========以上是排序========//
int main()
{
cin>>n;
for(int i=0;i<n;++i)
{
int shu;
cin>>shu;
heapadd(i,shu);
}
heapsort();
for(int i=0;i<n;++i)
cout<<num[i]<<" ";
return 0;
}
- 【kuandui】二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- 二叉堆
- Bartender打印和导出图片
- 【题解】最短路径四题
- 设计模式中类的关系
- 【转载】树状数组(简单)
- 上一个十年,下一个十年
- 【kuandui】二叉堆
- 对付内存改写问题的银弹
- 【最短路径】Floyd算法
- MIPS架构培训之Cache原理
- 【转载】最小生成树
- tomcat启动报java.io.EOFException错误
- MIPS CP0 Hazards问题--从Release1到Release2
- 【数据结构】树状数组
- MIPS架构培训之MMU原理