2018年5月3日 星期四

heap Sort (using Python)

  1. """ 
  2. heap Sort 
  3. """  
  4.   
  5. class Heap_Sort:  
  6.   def _Sort(self):  
  7.     for index in range(1,len(self._mlist)):    
  8.       if(self.End == index):  
  9.         break  
  10.       if(1 == index):  
  11.         continue  
  12.       while(1):  
  13.         parent = index//2  
  14.         if(self._mlist[parent]<self._mlist[index]):  
  15.           break  
  16.         self._mlist[parent],self._mlist[index],index=self._mlist[index],self._mlist[parent],parent  
  17.         if(not index!=1):  
  18.           break  
  19.           
  20.   def __init__(self,*array):  
  21.     self._mlist = list(array)  
  22.     self.End = 0  
  23.       
  24.   def print_heapSort(self):  
  25.     for index in range(len(self._mlist)-1,0,-1):  
  26.          self._mlist[self.End],self._mlist[index],self.End=self._mlist[index],self._mlist[self.End],index  
  27.          print([value for value in self._mlist if value])  
  28.          self._Sort()  
  29.       
  30.            
  31.   
  32. sort = Heap_Sort(0,23,23,45,15,15,32,47,3)  
  33. sort.print_heapSort()  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。