st0627 发表于 2018-8-15 11:23:46

[硕.Love Python] HeapSort(堆排序)

def adjust(a, root, n):  
    k = a
  
    c = root * 2
  

  
    while c <= n:
  
      if c + 1 <= n and a > a:
  
            c += 1
  

  
      if a <= k:
  
            break
  

  
      a = a
  
      c *= 2
  

  
    a = k
  

  
def heapSort(a):
  
    n = len(a)
  

  
    for i in xrange(n / 2, 0, -1):
  
      adjust(a, i, n)
  

  
    for i in xrange(n - 1, 0, -1):
  
      a, a = a, a
  
      adjust(a, 1, i)
  

  
if __name__ == '__main__':
  
    from random import shuffle
  

  
    data = range(100)
  
    shuffle(data)
  

  
    print data
  
    heapSort(data)
  
    print data
页: [1]
查看完整版本: [硕.Love Python] HeapSort(堆排序)