堆排序算法

1000(ms)
10000(kb)
2631 / 5595

编写程序堆排序算法。按照从小到大的顺序进行排序,测试数据为整数。

输入

第一行是待排序数据元素的个数; 第二行是待排序的数据元素。(提示:用小根堆)

输出

一趟堆排序的结果。

样例输入

10
50 36 41 19 23 4 20 18 12 22

样例输出

4 12 20 18 22 41 50 36 19 23
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std; void sift(int arr[],int low,int high)
{
int i=low,j=*i;
int tmp=arr[i];
while(j<=high)
{
if(j<high&&arr[j]>arr[j+]) //小的上浮,大的下沉
j++;
if(tmp>arr[j])
{
arr[i]=arr[j];
i=j;
j=*i;
}
else
break;
}
arr[i]=tmp;
} void Heapsort(int arr[],int n)
{
for(int i=n/;i>=;i--) //建立初始堆,第一趟
sift(arr,i,n);
} int main()
{
int n;
int arr[];
cin>>n;
for(int i=;i<=n;i++)
cin>>arr[i];
Heapsort(arr,n);
for(int i=;i<=n;i++)
cout<<arr[i]<<" ";
return ;
}
 

swust oj 1015的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. table表格内容溢出处理

    直接在table标签加上  style="table-layout:fixed;word-wrap:break-word;"

  2. Mybatis中使用集合、数组

    一.简述 本文讲Mybatis中如何将传入参数为数组或者集合对象,进行遍历,组合Where条件中如in条件等内容. 有3种情况: 如果传入的是单参数且参数类型是一个List的时候,collection ...

  3. ubuntu16.04 npm安装

    $ sudo apt-get install python-software-properties $ curl -sL https://deb.nodesource.com/setup_6.x | ...

  4. GDAL指定自定义的金字塔目录

    缘起 对于一般的遥感影像文件,金字塔文件默认都是与影像文件放在同一个目录下,金字塔文件名一般与源影像文件名相同,但后缀名不同.或者金字塔内建于影像内部,但这不是这里所涉及的. 在使用ArcGIS桌面版 ...

  5. shell符号解释

    #符号详解 () 在子shell中运行 (a=1);echo $a,结果是空,因为a=1不是在当前shell中运行的(a=1);(echo $a)也是空的 小技巧:(cd $path, do some ...

  6. 【nginx&php】后台权限认证方式

    一.最常用的方法(代码中限制) 1.如何限制IP function get_new_ip(){ if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('H ...

  7. WPF宝典Url

    https://sourceforge.net/directory/os:windows/https://archive.codeplex.com/ https://code.msdn.microso ...

  8. Window 包管理工具: chocolatey

    传送门 # 官网 https://chocolatey.org/install # 发生错误看看这个https://yevon-cn.github.io/2017/03/12/install-choc ...

  9. Atitit mysql 存储过程捕获所有异常,以及日志记录异常信息

    Atitit mysql 存储过程捕获所有异常,以及日志记录异常信息 1.1. 异常的处理模式exit  continue undo模式 1 1.2. 捕获所有异常使用        DECLARE ...

  10. Variable number of arguments (Varargs)

    A parameter of a function (normally the last one) may be marked with vararg modifier: fun <T> ...