题目地址: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 排序 -- 堆排序的更多相关文章

  1. 九度OJ 1202:排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...

  2. 【九度OJ】题目1202:排序 解题报告

    [九度OJ]题目1202:排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个 ...

  3. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  4. 【九度OJ】题目1023:EXCEL排序 解题报告

    [九度OJ]题目1023:EXCEL排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1023 题目描述: E ...

  5. 【九度OJ】题目1185:特殊排序 解题报告

    [九度OJ]题目1185:特殊排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1185 题目描述: 输入一系 ...

  6. 【九度OJ】题目1061:成绩排序 解题报告

    [九度OJ]题目1061:成绩排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1061 题目描述: 有N个学 ...

  7. 【九度OJ】题目1190:大整数排序 解题报告

    [九度OJ]题目1190:大整数排序 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1190 题目描述: 对N个长度最长可达 ...

  8. 【九度OJ】题目1196:成绩排序 解题报告

    [九度OJ]题目1196:成绩排序 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1196 题目描述: 用一维数组存储学号和成绩,然后 ...

  9. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

随机推荐

  1. HW2.25

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. POJ-3648 Wedding 2sat

    题目链接:http://poj.org/problem?id=3648‘ 题意:一对情人举行婚礼,有n对夫妇参加,别人对着坐在一个长桌子的两边,新娘和新郎坐在最后面,新娘只能看见坐在他对面的人.现在, ...

  3. cep

  4. python seq

    missing parentheses in call to print  ==> python高版本 print("") name 'raw_input' is not d ...

  5. 教程-Delphi操作快捷键

    ************************************************************** Delphi快捷键-全-高手用-南山古桃(新手)-同学共进 ******* ...

  6. VC++深入详解-第一章学习心得(一)

    句柄是系统为资源分配内存的标识号. 相当于一个指针指向一块内存空间,我暂时理解成一个地址,一个门牌号 HWND 窗口句柄 HICON 图标句柄 HCURSOR 光标句柄 HBRUSH 画刷句柄 消息的 ...

  7. yaxim

    Site: http://yaxim.org/yax.im/ Code: https://github.com/ge0rg/yaxim

  8. xcode针对不同IOS版本的代码编译问题

    有时候在项目中为了兼容低版本IOS系统,通常会针对不同的OS版本写不同的代码,例如: #define IS_IOS7_OR_LATER ([[UIDevice currentDevice].syste ...

  9. uuid_short() 源代码

    /* uuid_short handling. The short uuid is defined as a longlong that contains the following bytes: B ...

  10. JS问题Uncaught ReferenceError:XXXX is not defined

    背景: html中一个table,table中进行分页.每行后面有一系列操作,如删除,修改.现在以删除为例说明问题. 实现方式: 使用button,在onclick中调用js函数,js函数中传递参数如 ...