Turbo Sort Add problem to Todo list Problem code: TSORT
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的更多相关文章
- 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 ...
- 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 ...
- 【CodeChef】Turbo Sort
题目链接:Turbo Sort 用java自带O(NlogN)的排序就可以,java要特别注意输入输出.输入用BufferedReader,输出用printWriter.printWriter的速度比 ...
- Problem : 1002 ( A + B Problem II )
经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...
- 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 ...
- 几何入门合集 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个 ...
- Problem B The Blocks Problem(vector的使用)
题目链接:Problem B 题意:有n块木块,编号为0~n-1,要求模拟以下4种操作(下面的a和b都是木块编号) 1. move a onto b: 把a和b上方的木块全部归位,然后把a摞在b上面. ...
- 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 ...
- Problem J. Joseph’s Problem 约瑟夫问题--余数之和
链接:https://vjudge.net/problem/UVA-1363 题意:给出n k,当 i 属于 1~n 时 ,求解 n% i 的和 n 和 k 的范围都是 1 到 10^9; 商相同 ...
随机推荐
- C语言初学 数学中带根号的复杂计算问题
#include<stdio.h> #include<math.h> int main() { double a,b; scanf("%lf%lf",&am ...
- ios如何判断键盘是否已经显示
ios如何判断键盘是否已经显示 在群里看到有人问:ios如何判断键盘已经显示在界面上. 其实这个解决很简单: 写一个单例来管理键盘的状态. 这个单例在初始化方法init种监听2个事件,分别是 UI ...
- VMware vSphere 6 Enterprise Plus License
Product: VMware vSphere 6 Enterprise Plus Licensed for 2 physical CPUs (unlimited cores per CPU) Lic ...
- android资料
http://bbs.51cto.com/thread-903936-1.html http://zhidao.baidu.com/question/195697097.html?sort=4& ...
- cf B. Jeff and Periods
http://codeforces.com/contest/352/problem/B #include <cstdio> #include <cstring> #includ ...
- zabbix 添加自定义key
vim /etc/zabbix/zabbix_agentd.conf UserParameter=zjzc.login,/bin/sh /usr/sbin/get_login.sh UserParam ...
- java基础进阶:SQL的运用
SQL的基础的运用 /* --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Cour ...
- socket 网摘
一.基本socket函数 Linux系统是通过提供套接字(socket)来进行网络编程的.网络的socket数据传输是一种特殊的I/O,socket也是一种文件描述符.socket也有一个类似于打 开 ...
- c语言函数定义、函数声明、函数调用以及extern跨文件的变量引用
1.如果没有定义,只有声明和调用:编译时会报连接错误.undefined reference to `func_in_a'2.如果没有声明,只有定义和调用:编译时一般会报警告,极少数情况下不会报警告. ...
- stardict词典(星际译王)
sudo apt-get install stardict 下载词库: http://abloz.com/huzheng/stardict-dic/zh_CN/ 把下载的压缩包解压,以a为例cd /u ...