堆排序 HeapSort
2016-02-20 20:47
369 查看
堆排序
#include<iostream> using namespace std; void HeapAdjust(int H[],int s,int m) { int rc=H[s]; for(int j=2*s;j<m;j*=2) { if(j<m&&H[j]<H[j+1]) j++; if(rc>H[j]) break; H[s]=H[j]; s=j; } H[s]=rc; } void HeapSort(int H[],int H_length) { for(int i=H_length/2;i>0;i--) HeapAdjust(H,i,H_length); for(int i=H_length,temp;i>1;--i) { temp=H[1]; H[1]=H[i]; H[i]=temp; HeapAdjust(H,1,i-1); } } int main() { int H[100]={0},n,i=1; int H_length=0; cout<<"请输入需要排序的数,并以0作为结束"<<endl; while(cin>>n) { if(n==0) break; H[i]=n; i++; } H_length=i-1; HeapSort(H,H_length); for(i=1;i<=H_length;i++) { cout<<H[i]<<" "; } }
相关文章推荐
- jquery常见问题
- 一路走来
- 图片加载库Glide用法
- iOS中一个tableView中显示两种cell的问题
- .a静态库的制作 Debug版和Release版
- C++学习之 构造函数中调用构造函数
- altium designer 重新给文件命名
- 回首2015,展望2016
- 9-题目1186:打印日期
- xpath
- 静态和动态函数库设计
- 函数定义
- git 版本落后推送错误
- int const * p1和 const int * p1的区别
- 截取View当前的图像保存成View
- MySQL_控制台操作_02
- git分支的合并
- <<Effective TCP/IP Programming>>读书笔记
- 三样献瑞
- 查找——图文翔解Treap(树堆)