Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1790692
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-04-19 20:19:53

深感自己实现的够丑陋,照着伪代码写下来的。

点击(此处)折叠或打开

  1. #http://rosettacode.org/wiki/Sorting_algorithms/Heapsort
  2. def heapSort(seq,count):
  3.     #first place a in max-heap order
  4.     heapify(seq,count)
  5.     end=count-1
  6.     while end > 0:
  7.         seq[end],seq[0]=seq[0],seq[end]
  8.         end-=1
  9.         #put the heap back in max-heap order
  10.         siftDown(seq,0,end)

  11. def heapify(seq,count):
  12.     start=count//2
  13.     while start>=0:
  14.         siftDown(seq,start,count-1)
  15.         start-=1

  16. def siftDown(seq,start,end):
  17.     #end represents the limit of how far down the heap to sift
  18.     root=start
  19.     while root*2 + 1 <= end:
  20.         child=root*2+1
  21.         if child+1 <= end and seq[child] < seq[child+1]:
  22.             child+=1
  23.         if seq[root] < seq[child]:
  24.             seq[root],seq[child]=seq[child],seq[root]
  25.             root=child
  26.         else:
  27.             return

  28. def test_heapsort():
  29.     import random
  30.     import copy
  31.     ll=[random.randint(1,50) for i in range(20)]
  32.     ll2=copy.deepcopy(ll)
  33.     print(ll)
  34.     heapSort(ll,len(ll))
  35.     print("after heapSort:",ll)
  36.     print(ll==sorted(ll2))

还是看看别人实现的吧。

点击(此处)折叠或打开

  1. __author__ = "lynn lin"
  2. __lience__ = "MIT"
  3. #heapq is priority queue
  4. class sort_collection:
  5.     
  6.     def __init__(self,A=[]):
  7.         self.data=A
  8.         self.heapsize=len(A)

  9.     def __left(self,i):
  10.         return (2*i+1)

  11.     def __right(self,i):
  12.         return (2*i+2)

  13.     def __parent(self,i):
  14.         return (i-1)//2

  15.     def __max_heapify(self,i):
  16.         left=self.__left(i)
  17.         right=self.__right(i)
  18.         if left < self.heapsize and self.data[left] > self.data[i]:
  19.             largest=left
  20.         else:
  21.             largest=i
  22.         if right < self.heapsize and self.data[right] > self.data[largest]:
  23.             largest=right
  24.         if largest!=i:
  25.             self.data[i],self.data[largest]=self.data[largest],self.data[i]
  26.             self.__max_heapify(largest)

  27.     def __min_heapify(self,i):
  28.         left=self.__left(i)
  29.         right=self.__right(i)
  30.         if left < self.heapsize and self.data[left] < self.data[i]:
  31.             smallest=left
  32.         else:
  33.             smallest=i
  34.         if right < self.heapsize and self.data[right] < self.data[smallest]:
  35.             smallest=right
  36.         if smallest!=i:
  37.             self.data[i],self.data[smallest]=self.data[smallest],self.data[i]
  38.             self.__min_heapify(smallest)

  39.     def build_max_heapify(self):
  40.         for i in range(len(self.data)//2,-1,-1):
  41.             self.__max_heapify(i)

  42.     def build_min_heapify(self):
  43.         for i in range(len(self.data)//2,-1,-1):
  44.             self.__min__heapify(i)

  45.     def heapsort(self,mode=1):
  46.         #default sort order is ascending,if setting mode to 0,the sort order is descending
  47.         if mode==1:
  48.             self.build_max_heapify()
  49.         else:
  50.             self.build_min_heapify()

  51.         length=len(self.data)
  52.         for i in range(length-1,0,-1):
  53.             self.data[i],self.data[0]=self.data[0],self.data[i]
  54.             self.heapsize-=1
  55.             if mode==1:
  56.                 self.__max_heapify(0)
  57.             else:
  58.                 self.__min_heapify(0)

  59.     def heap_maximum(self):
  60.         return self.data[0]

  61.     def heap_minimum(self):
  62.         return self.data[0]

  63.     def heap_extract_min(self):
  64.         if self.heapsize < 1:
  65.             raise Exception("heap empty")
  66.         smallest=self.data[0]
  67.         self.data[0]=self.data[self.heapsize-1]
  68.         self.heapsize-=1
  69.         self.__min_heapify(0)
  70.         return smallest

  71.     def heap_extract_max(self):
  72.         if self.heapsize < 1:
  73.             raise Exception("heap empty")
  74.         biggest=self.data[0]
  75.         self.data[0]=self.data[self.heapsize-1]
  76.         self.heapsize-=1
  77.         self.__max_heapify(0)
  78.         return biggest

  79.     def heap_increase_key(self,i,key):
  80.         if key < self.data[i]:
  81.             raise
  82.         self.data[i]=key
  83.         while i > 0 and self.data[self.__parent(i)] < self.data[i]:
  84.             self.data[self.__parent(i)],self.data[i]=self.data[i],self.data[self.__parent(i)]
  85.             i=self.__parent(i)

  86. def test_sort_collection():
  87.     import random
  88.     l=[random.randint(1,50) for i in range(20)]
  89.     ll=sort_collection(l)
  90.     ll.build_max_heapify()
  91.     #ll.heapsort()
  92.     print(ll.data)
  93.     ma=ll.heap_extract_max()
  94.     mb=ll.heap_extract_max()
  95.     mc=ll.heap_maximum()
  96.     print(ma,mb,mc,sep=":")

  97. test_sort_collection()
附赠代码 Sieve of Eratosthenes,感慨下这个网站 http://rosettacode.org 真不错。

点击(此处)折叠或打开

  1. #http://rosettacode.org/wiki/Sieve_of_Eratosthenes
  2. def sieve_algorithm(number):
  3.     is_prime=[False]*2+[True]*(number-1)
  4.     for n in range(int(number**0.5+1.5)):
  5.         if is_prime[n]:
  6.             for i in range(n*n,number+1,n):
  7.                 is_prime[i]=False
  8.     return [i for i , prime in enumerate(is_prime) if prime]



阅读(1501) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~