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 ary10 11 12 def max_heapify(ary, start, end):13 root = start14 while True:15 child = root * 2 + 116 if child > end:17 break18 if child + 1 <= end and ary[child] < ary[child + 1]:19 child += 120 if ary[root] < ary[child]:21 ary[root], ary[child] = ary[child], ary[root]22 root = child23 else:24 break25 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()就影响了
1 def main(): 2 # ary = [6, 5, 3, 1, 8, 7, 2, 4] 3 #heap_sort(ary) 4 5 #print(ary) 6 7 n = int(sys.stdin.readline()) 8 ary = [] # 申明一个数组 9 10 while n > 0:11 ary.append(int(raw_input())) # 输入的排成数组12 n -= 113 14 ary.sort() #自带两种排序算法,.sort是真是变,sorter()是表面变15 print '/n'.join(map(str, ary)) # map函数就是一个映射/转换,把list转换成string16 17 18 main()
学习
挑战了一把当年讳莫如深的堆排,现在理解其实不难,就是一个使用二叉来减少比较次数的快速排序
各种py排序算法
http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/
熟悉了通过查看文档来学习使用python的相关函数
熟悉调用其自生的函数库
新闻热点
疑难解答