Turbo Sort Add problem to Todo list Problem code: TSORT

 1 def heap_sort(ary):
 2     n = len(ary)  # 8
 3     first = int(n / 2 - 1)  # 3
 4     for start in range(first, -1, -1):  # 3~0 revese
 5         max_heapify(ary, start, n - 1)  # from start
 6     for end in range(n - 1, 0, -1):
 7         ary[end], ary[0] = ary[0], ary[end]
 8         max_heapify(ary, 0, end - 1)
 9     return ary
10
11
12 def max_heapify(ary, start, end):
13     root = start
14     while True:
15         child = root * 2 + 1
16         if child > end:
17             break
18         if child + 1 <= end and ary[child] < ary[child + 1]:
19             child += 1
20         if ary[root] < ary[child]:
21             ary[root], ary[child] = ary[child], ary[root]
22             root = child
23         else:
24             break
25
26
27 def main():
28     ary = [6, 5, 3, 1, 8, 7, 2, 4]
29     heap_sort(ary)
30
31     print(ary)
32
33 main()

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

学习

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

  各种py排序算法

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

时间: 2024-11-06 09:34:09

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

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

1 import sys 2 3 4 def count_holes(letter): 5 hole_2 = ['A', 'D', 'O', 'P', 'Q', 'R'] 6 if letter == 'B': 7 return 2 8 elif letter in hole_2: 9 return 1 10 else: 11 return 0 12 13 14 def main(): 15 n = int(sys.stdin.readline()) 16 for t in sys.stdin:

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

1 '''def count_lead(first, second): 2 if first > second: 3 return 1, first - second 4 elif first == second: # 题目中没有说明相等的情况 5 return 0, 0 6 else: 7 return 2, second - first''' 8 9 10 def main(): 11 n = int(raw_input()) 12 lead = 0 13 winner = 0 # 有些初始

【CodeChef】Turbo Sort

题目链接:Turbo Sort 用java自带O(NlogN)的排序就可以,java要特别注意输入输出.输入用BufferedReader,输出用printWriter.printWriter的速度比System.out快很多,参考StackOverflow. 代码: 1 import java.io.BufferedOutputStream; 2 import java.io.BufferedReader; 3 import java.io.InputStreamReader; 4 impor

codechef Turbo Sort 题解

Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7 大数据的排序,输入和输出. 一开始使用了cou

NCPC 2015 October 10, 2015 Problem D

NCPC 2015Problem DDisastrous DowntimeProblem ID: downtimeClaus Rebler, cc-by-saYou’re investigating what happened when one ofyour computer systems recently broke down. So faryou’ve concluded that the system was overloaded; itlooks like it couldn’t ha

hidden node and exposed node problem

Exposed node problem In wireless networks, theexposed node problem occurs when a node is prevented from sending packets to other nodes because of a neighboring transmitter. Consider an example of 4 nodes labeled R1, S1, S2, and R2, where the two rece

fzuoj Problem 2236 第十四个目标(树状数组+dp)

Problem 2236 第十四个目标  Problem Description 目暮警官.妃英里.阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶.在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关. 为了避免下一个遇害者的出现,柯南将可能遭到暗算的人中的数字按关联程度排列了出来,即顺序不可改变.柯南需要知道共有多少种可能结果,满足受害人名字出现的数字严格

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe

FZU 2215 Simple Polynomial Problem (多项式乘法 栈)

Problem 2215 Simple Polynomial Problem Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description You are given an polynomial of x consisting of only addition marks, multiplication marks, brackets, single digit numbers, and of course the le