九度OJ 1202 排序 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1202
- 题目描述:
-
对输入的n个数进行排序并输出。
- 输入:
-
输入的第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。
- 输出:
-
可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。
每组测试数据的结果占一行。
- 样例输入:
-
4
1 4 3 2
- 样例输出:
-
1 2 3 4
#include <stdio.h>
#include <stdlib.h> void HeapSort(int Array[], int len);
void BuildMaxHeap(int Array[], int len);
void ShiftDown(int Array[], int index, int len);
void Swap(int *m, int *n); int main(void){
int len;
int * Array;
int i; while (scanf("%d", &len) != EOF){ //读入数据个数
Array = (int *)malloc(sizeof(int) * (len+1));
for (i=1; i<=len; ++i) //读入数据
scanf("%d", &Array[i]); BuildMaxHeap(Array, len);
HeapSort(Array, len);
for (i=1; i<=len; ++i)
printf("%d ", Array[i]);
printf("\n");
free(Array);
}
return 0;
} void HeapSort(int Array[], int len){
int i;
int time = len;
for (i=0; i<len; ++i){
Swap(&Array[1], &Array[time]);
--time;
ShiftDown(Array, 1, time);
}
} void BuildMaxHeap(int Array[], int len){
int index = len/2; for (; index>0; --index){
ShiftDown(Array, index, len);
}
}
void ShiftDown(int Array[], int index, int len){
while ((2*index) <= len || (2*index+1) <= len){
if (2*index+1 <= len){
if (Array[index] < Array[2*index] ||
Array[index] < Array[2*index+1]){
if (Array[2*index] > Array[2*index+1]){
Swap(&Array[index], &Array[2*index]);
index = 2*index;
}
else{
Swap(&Array[index], &Array[2*index+1]);
index = 2*index + 1;
}
}
else
break;
}
else{
if (Array[index] < Array[2*index]){
Swap(&Array[index], &Array[2*index]);
index = 2*index;
}
else
break;
}
}
} void Swap(int *m, int *n){ //交换数据
int tmp;
tmp = *m;
*m = *n;
*n = tmp;
}
参考资料:http://blog.csdn.net/v_july_v/article/details/6198644
九度OJ 1202 排序 -- 堆排序的更多相关文章
- 九度OJ 1202:排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...
- 【九度OJ】题目1202:排序 解题报告
[九度OJ]题目1202:排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 【九度OJ】题目1023:EXCEL排序 解题报告
[九度OJ]题目1023:EXCEL排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1023 题目描述: E ...
- 【九度OJ】题目1185:特殊排序 解题报告
[九度OJ]题目1185:特殊排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1185 题目描述: 输入一系 ...
- 【九度OJ】题目1061:成绩排序 解题报告
[九度OJ]题目1061:成绩排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1061 题目描述: 有N个学 ...
- 【九度OJ】题目1190:大整数排序 解题报告
[九度OJ]题目1190:大整数排序 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1190 题目描述: 对N个长度最长可达 ...
- 【九度OJ】题目1196:成绩排序 解题报告
[九度OJ]题目1196:成绩排序 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1196 题目描述: 用一维数组存储学号和成绩,然后 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
随机推荐
- boj1267 Infinite’s Cave 树形dp + 背包
题目链接:http://acm.bupt.edu.cn/onlinejudge/newoj/showProblem/show_problem.php?problem_id=1267 / ...
- 局域网yum服务器创建
yum createrepo createrepo dir 配置httpd发布yum-repo; 在客户端添加yum.rep配置文件;
- Away3D 的实体收集器Bug
最近在改Away3D源码的时候遇到个很郁闷的问题,发现创建的Mesh 释放不掉. 分析源码发现 EntityListItemPool 类中逻辑Bug在getItem()函数中发现_poolSize 对 ...
- java web知识点总结
创建与销毁 ServletContext HttpRequest HttpSession 1.ServletContext 创建:启动服务器时就创建,服务为每个web应用创建该项目的ServleCon ...
- JSF学习四 标签
commandButton:提交.重置或下压button button:用于公布GET请求的按钮
- 浅谈 Underscore.js 中 _.throttle 和 _.debounce 的差异
Underscore.js是一个很精干的库,压缩后只有5.2KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程. 本文仅探讨Underscore.js的两个 ...
- [React] React Fundamentals: Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
- Kinect for Windows V2和V1对照开发___深度数据获取并用OpenCV2.4.10显示
V1深度分辨率:320x240 V2深度分辨率:512x424 1. 打开深度图像帧的方式 对于V1: hr = m_PNuiSensor->NuiImageStreamOpen( NUI_I ...
- phpmailer使用gmail SMTP的方法
终于能够通过phpmailer使用gmail账号发送邮件了phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它. 但是, ...
- careercup-递归和动态规划 9.10
9.10 给你一堆n个箱子,箱子宽w,高h,深d.箱子不能翻转,将箱子堆起来时,下面箱子的宽度.高度和深度必须大于上面的箱子.实现一个方法,搭出最高的一堆箱子,箱堆的高度为每个箱子高度的总和. 解法: ...