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而被提出.除了堆排序,“堆”这种数据结构还可以用于优先队列的实现. 堆首先是一个完全二叉树:它除了最底层之外,树的每一层的都 ...
随机推荐
- 获取datatable更新之前的数据
string dd = ds.Tables[0].Rows[0][0, DataRowVersion.Original].ToString() ;
- spring的懒加载
在spring的IOC容器中,可以通过设置<beans default-lazy-init="XXX"></beans>来设置是否为懒加载模式,懒加载的意思 ...
- hibernate4整合spring3事务问题
本文是作者在对hibernate4+spring3+struts2整合中遇到的一个问题.对s2sh进行了基本的整合搭建以后,就是对事务的控制管理,将hibernate的事务交由spring管理.根据网 ...
- java开发过程中从前台传到后台中文乱码《filter》
在企业开发中,最常见的是javaweb项目,有web项目就免不了和后台打交道,比如我从jsp页面发送新增请求到后台,后台可能是servlet.struts2.springmvc等,这时就存在一个问题, ...
- 通过GPS数据反向地理信息编码, 得到当前位置信息
检查可用性 这属于基础知识, 不赘述, 总的来说,你的设备的支持要打开, 添加CoreLocation的framework, 引用头文件, 添加委托,然后, 好的实践是在使用前编程检查相关可用性: - ...
- angularjs 的ng-bind-html过滤了内容的style
1.不使用ng-bind-html,使用trustAs() trustAsHtml(value) = trustAs($sce.HTML, value) trustAsJs(value) = ...
- Android破解之Lic文件加密程序(首例)
我不会写Android,这是我第一个破解Android的例子,耗时接近一天,希望大神不要见笑! 本程序为商业软件,不便发布APK程序. 不要给我发消息,我不得回,有问题,直接回帖就可以了. 准备工作 ...
- SQL SERVER – Attach mdf file without ldf file in Database
Background Story: One of my friends recently called up and asked me if I had spare time to look at h ...
- 深入理解Activity-任务,回退栈,启动模式
一.任务.回退栈的概念 一个任务是多个能够和用户进行交互并且能完成某种功能的activities的集合. 这些Activity被安排回退栈中,能在合适的时候被打开显示出来. 在我们使用Android手 ...
- 安卓第十天笔记-fragment
安卓第十天笔记-fragment Fragment(片段) 一.Fragment简介 *Fragment是3.0引入的API,主要为了解决平板,大屏幕手机显示问题 *Fragment代表了Activi ...