def heap_sort(ary):
n = len(ary) #
first = int(n / 2 - 1) #
for start in range(first, -1, -1): # 3~0 revese
max_heapify(ary, start, n - 1) # from start
for end in range(n - 1, 0, -1):
ary[end], ary[0] = ary[0], ary[end]
max_heapify(ary, 0, end - 1)
return ary def max_heapify(ary, start, end):
root = start
while True:
child = root * 2 + 1
if child > end:
break
if child + 1 <= end and ary[child] < ary[child + 1]:
child += 1
if ary[root] < ary[child]:
ary[root], ary[child] = ary[child], ary[root]
root = child
else:
break def main():
ary = [6, 5, 3, 1, 8, 7, 2, 4]
heap_sort(ary) print(ary) main()

//Py自带的两种算法,一个sorted(ary)不影响本身结构,可ary.sort()就影响了

 def main():
# ary = [6, 5, 3, 1, 8, 7, 2, 4]
#heap_sort(ary) #print(ary) n = int(sys.stdin.readline())
ary = [] # 申明一个数组 while n > 0:
ary.append(int(raw_input())) # 输入的排成数组
n -= 1 ary.sort() #自带两种排序算法,.sort是真是变,sorter()是表面变
print '\n'.join(map(str, ary)) # map函数就是一个映射/转换,把list转换成string main()

学习

  挑战了一把当年讳莫如深的堆排,现在理解其实不难,就是一个使用二叉来减少比较次数的快速排序

  各种py排序算法

    http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/

  熟悉了通过查看文档来学习使用python的相关函数

  熟悉调用其自生的函数库

Turbo Sort Add problem to Todo list Problem code: TSORT的更多相关文章

  1. Holes in the text Add problem to Todo list Problem code: HOLES

    import sys def count_holes(letter): hole_2 = ['A', 'D', 'O', 'P', 'Q', 'R'] if letter == 'B': return ...

  2. The Lead Game Add problem to Todo list Problem code: TLG

    '''def count_lead(first, second): if first > second: return 1, first - second elif first == secon ...

  3. 【CodeChef】Turbo Sort

    题目链接:Turbo Sort 用java自带O(NlogN)的排序就可以,java要特别注意输入输出.输入用BufferedReader,输出用printWriter.printWriter的速度比 ...

  4. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

  5. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem B. Travelling Camera Problem set贪心

    Problem B. Travelling Camera Problem 题目连接: http://www.codeforces.com/gym/100253 Description Programm ...

  6. 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi

    abstract: V const & a 加速 F. Mirror 题意 链接 问题: 有n个人在y=0的平面上(及xoz平面).z=0平面上有一面镜子(边平行于坐标轴).z=a平面上有q个 ...

  7. Problem B The Blocks Problem(vector的使用)

    题目链接:Problem B 题意:有n块木块,编号为0~n-1,要求模拟以下4种操作(下面的a和b都是木块编号) 1. move a onto b: 把a和b上方的木块全部归位,然后把a摞在b上面. ...

  8. A+B Problem Plus and A-B Problem Plus and A*B Problem Plus

    //we have defined the necessary header files here for this problem. //If additional header files are ...

  9. Problem J. Joseph’s Problem 约瑟夫问题--余数之和

    链接:https://vjudge.net/problem/UVA-1363 题意:给出n  k,当 i 属于 1~n 时 ,求解 n% i 的和 n 和 k 的范围都是 1 到 10^9; 商相同 ...

随机推荐

  1. C#文本文件导入数据库

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...

  2. iOS学习之懒加载

    懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,其实是重写getter方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化 使用懒 ...

  3. 职员时序安排lingo求解

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !职员时序安排模型 题目: 一项工作一周七天都需要有人,每天所需的最少职工数为20,16,13,1 ...

  4. matlab 对图像操作的函数概览

    转自博客:http://blog.163.com/fei_lai_feng/blog/static/9289962200991713415422/ 一. 读写图像文件 1. imread imread ...

  5. VMware vSphere 6 Enterprise Plus License

    Product: VMware vSphere 6 Enterprise Plus Licensed for 2 physical CPUs (unlimited cores per CPU) Lic ...

  6. cf D. Sereja ans Anagrams

    http://codeforces.com/contest/368/problem/D #include <cstdio> #include <cstring> #includ ...

  7. Linux 杀死挂起的进程

    在用管理员执行一个命令后,用Ctrl+Z把命令转移到了后天.导致无法退出root的. 输入命令:exit终端显示:There are stopped jobs. 解决方法:方法一.输入命令:jobs终 ...

  8. 为什么memset的第二个参数不把int替换成char

    memset是一个经常被用来初始化数组的函数,其定义如下: 1 void * memset ( void * ptr, int value, size_t num ); 它的效果大致是把以ptr为起始 ...

  9. Teach Yourself Scheme in Fixnum Days 13 Jump跳转

    Jumps One of the signal features of Scheme is its support for jumps or nonlocal control. Specificall ...

  10. 读书笔记:java并发

    java中主要的同步机制是关键字synchronized,它提供一种独占锁,但是 同步这个术语还包括validate类型的变量,显示锁(Explicit Lock)以及原子变量. -------显示锁 ...