基本排序算法--堆排序
2015-08-25 16:36
344 查看
#include<iostream> using namespace std; void HeapAdjust(int a[], int n,int i) { int left = 2 * i; int right = 2 * i + 1; int max = i; if (left < n&&a[max] < a[left]) max = left; if (right < n&&a[max] < a[right]) max = right; if (max != i) { int temp = a[max]; a[max] = a[i]; a[i] = temp; HeapAdjust(a, n, max); } } void BuildHeap(int a[], int n) { for (int i = n / 2 - 1; i >= 0; i--) HeapAdjust(a, n, i); } void HeapSort(int a[], int n) { BuildHeap(a, n); for (int i = n - 1; i > 0; i--) { int temp = a[0]; a[0] = a[i]; a[i] = temp; HeapAdjust(a, i, 0); } } int main() { int array[] = { 34, 65, 12, 43, 67, 5, 78, 10, 3, 70 }; int len = sizeof(array) / sizeof(int); cout << "the original array are:" << endl; for (int k = 0; k < len; k++) cout << array[k] << " "; cout << endl; HeapSort(array, len); cout << "The sorted array are:" << endl; for (int k = 0; k<len; k++) cout << array[k] << ","; cout << endl; return 0; }
相关文章推荐
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
- 如何修改linux 的SSH的默认端口号
- 数据结构之红黑树——BST的变种
- Win7 64位 + LoadRunner 11录制时弹不出IE的解决办法
- iOS----时间日期处理
- iOS UIWebView 通过 cookie 完成自动登录验证
- android的DrawerLayout用法
- struts2之文件上传
- 【POJ 3615】Cow Hurdles
- Android meta-data(未完成)
- C#对Excel的操作
- LINUX 使用DBCA创建ORACLE数据库
- 显示Servlet API主要版本,次要版本以及服务器系统信息
- Hibernate + jbosstools 整合、配置、测试详解
- js,jquery错误总结(更新中)
- OpenStreetMap/Google/百度/Bing瓦片地图服务(TMS)
- Android 程序打包及签名
- android学习笔记5 - SharedPreferences数据存储技术
- Android反射
- service zabbix does not support chkconfig