Python HeapSort
__author__ = 'student'
print 'hello world hello python' '''
heap sort
root leftchild 2n+1 rightchild 2n+2
compare them and get the maxnode
step by step think way
one step write the perfect program is hard
but it is easy to write your think step by step
build max heap
then swap the biggest number with the size
heap sort is a tuning for selection sort.
'''
la =[1,5,7,3,20,0,9,4]
print ', '.join(str(x) for x in la)
#from bottom to top
def heap(la,root,heap_size=None):
if heap_size is None:
length=la.__len__()
else:
length=heap_size
lc=root*2+1
rc=root*2+2
maxnode=root
if length>lc and la[lc]>la[root]:
maxnode=lc
if length>rc and la[rc]>la[maxnode]:
maxnode=rc
if maxnode!=root:
la[maxnode],la[root]=la[root],la[maxnode]
heap(la,maxnode,heap_size) #build max heap
def build_max_heap(la):
root=la.__len__()/2-1
while root>=0:
heap(la,root)
root-=1
#heap sort
#print ','.join (str(x) for x in xrange(la.__len__(),0,-1))
build_max_heap(la) def heap_sort(la):
heap_size=la.__len__()-1
for i in xrange(heap_size,0,-1):
la[i],la[0]=la[0],la[i] #swap
heap(la,0,heap_size)
heap_size-=1
print heap_size,la
print la
heap_sort(la) 感悟:
学算法千万不能背,不能去抄别人的代码。
首先要去理解整个逻辑,找一个小的数据集,自己推算出其过程,
然后根据这个过程来写代码。
否则抄别人的代码,被别人的思路牵着走,最后容易忘记,还得回头学。
Python HeapSort的更多相关文章
- 基本排序算法的Python实现
本篇主要实现九(八)大排序算法,分别是冒泡排序,插入排序,选择排序,希尔排序,归并排序,快速排序,堆排序,计数排序.希望大家回顾知识的时候也能从我的这篇文章得到帮助. 为了防止误导读者,本文所有概念性 ...
- Python学习路程day17
常用算法与设计模式 选择排序 时间复杂度 二.计算方法 1.一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费 ...
- 八大排序算法的 Python 实现
转载: 八大排序算法的 Python 实现 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 描述 插入排序的基本操作就是将一个 ...
- 经典排序算法总结与实现 ---python
原文:http://wuchong.me/blog/2014/02/09/algorithm-sort-summary/ 经典排序算法在面试中占有很大的比重,也是基础,为了未雨绸缪,在寒假里整理并用P ...
- algorithm: heap sort in python 算法导论 堆排序
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to ...
- Python之路,Day21 - 常用算法学习
Python之路,Day21 - 常用算法学习 本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...
- Heapsort 堆排序算法详解(Java实现)
Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...
- 常用排序算法的python实现和性能分析
常用排序算法的python实现和性能分析 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试 ...
- 最小堆实现优先队列:Python实现
最小堆实现优先队列:Python实现 堆是一种数据结构,因为Heapsort而被提出.除了堆排序,“堆”这种数据结构还可以用于优先队列的实现. 堆首先是一个完全二叉树:它除了最底层之外,树的每一层的都 ...
随机推荐
- 大话设计模式-->模板方法设计模式
在学习java的过程中,我们肯定听到过设计模式这名词,在行业中有这么一句话,若您能熟练的掌握23种设计模式,那么你便是大牛! 好了,废话不多说,今天我跟大家分享一下23种设计模式之一的 模板方法 设 ...
- Ajax,谷歌提示AutoCompleteExtender控件
提示内容从数据库中读取: ------------------------------------------页面 <asp:ScriptManager ID="ScriptManag ...
- Discuz DB层跨库映射关系表名前缀BUG修复后产生的新bug
新的逻辑引入了新的bug,会导致在跨多库连接时,产生表名前缀映射混乱,需要再做逻辑上的修复. function table_name($tablename) { if(!empty($this-> ...
- IBatis 配置一对多
-------说明-------- IBatis 版本2.0 配置一对多 namespace = testDao ------------------ /** *班级的resultMap *Class ...
- XLConnect:一个用R处理Excel文件的高效平台
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- Python tools for Visual Studio插件介绍
Python tools for Visual Studio是一个免费开源的VisualStudio的插件,支持 VisualStudio 2010,2012与2013.我们想要实现的是: ...
- linux backlog深入剖析以及netty设置backlog
netty不同于socket,其上次API没有提供设置backlog的选项,而是依赖于操作系统的somaxconn和tcp_max_syn_backlog,对于不同OS或版本,该值不同,建议根据实际并 ...
- c++之函数重载(函数匹配)
Case void f(); void f(int); void f(int, int); void f(double, double = 3.14); 匹配原则: 1)其形参数量与本次调用提供的实参 ...
- 【GOF23设计模式】策略模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_策略模式.CRM中报价策略.GUI编程中布局管理器底层架构 package com.test.strategy; /** ...
- 领域对象模型(domain object model)
在Play程序中,模型(model)占据了核心地位.它是程序操作的信息的特定领域的表现方式. Martin Fowler这样定义模型: 负责表达业务概念,业务状态信息以及业务规则.尽管保存业务状态的技 ...