堆排序
2015-06-03 17:28
288 查看
#include<iostream> #include<vector> using namespace std; int heapAdjust(int array[],int top,int depth) { int rc=array[top]; for(int i=top*2;i<=depth;i*=2) { if(i<depth && array[i]<array[i+1]&&(i+1)<=depth) ++i; if(rc>array[i]) break; array[top]=array[i]; top=i; } array[top]=rc; } void heapSort(int array[],int length) { int i=0; for(i=length/2;i>0;--i) { heapAdjust(array,i,length); } for(i=length;i>1;--i) { int temp=array[1]; array[1]=array[i]; array[i]=temp; heapAdjust(array,1,i-1); } } int main() { //第一次不算,因为是从下标1开始排序的。 int a[9]={0,49,38,65,97,76,13,27,49}; for(int i=0;i<9;i++) cout<<a[i]<<" "; cout<<endl; heapSort(a,8); // heapAdjust(a,2,8); for(int i=0;i<9;i++) cout<<a[i]<<" "; cout<<endl; // system("pause"); return 0; }
相关文章推荐
- JSP,PHP,Python,Ruby,Perl概要及各自特点
- 学生信息管理系统之问题集锦
- 程序员的量化交易之路(14)--Cointrader数据表(2)
- linux php下Ajax请求时URL路径出现重复
- android activity
- Maven学习 (四) 使用Nexus搭建Maven私服
- Maven学习 (六) 搭建多模块企业级项目
- 【ContestHunter】【弱省胡策】【Round2】
- Android培训的未来发展空间怎么样呢?
- PHP 在windows下配置sendmail,通过 mail() 函数发送邮件
- LeetCode Unique Paths(dp)
- WIndows下Python-Scrapy安装
- 【C#】读书笔记
- git删除fork后的项目
- 为什么这段代码打印出来的不是数字,而是地址,链表插入的?
- Mac Yosemite 使用
- 使用valgrind的callgrind工具进行多线程性能分析
- Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
- srm 531
- js数字或字母的正则表达式