def adjust(a, root, n): k = a[root-1] c = root * 2 while c <= n: if c + 1 <= n and a[c] > a[c-1]: c += 1 if a[c-1] <= k: break a[c/2-1] = a[c-1] c *= 2 a[c/2-1] = 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[0], a[i] = a[i], a[0] adjust(a, 1, i) if __name__ == '__main__': from random import shuffle data = range(100) shuffle(data) print data heapSort(data) print data
劉碩老師Python精品課程:
《Python高級編程技巧實戰》:
http://coding.imooc.com/class/62.html
《Python算法實戰視頻課程》:
http://study.163.com/course/courseMain.htm?courseId=1003617013
《Python科學計算—NumPy實戰課程》:
http://edu.51cto.com/course/course_id-5046.html
熊貓TV直播間: