Understanding how to create a heap in Python

前端 未结 4 2033
生来不讨喜
生来不讨喜 2020-12-23 14:32

The collections.Count.most_common function in Python uses the heapq module to return the count of the most common word in a file, for instance.

4条回答
  •  [愿得一人]
    2020-12-23 15:23

    this is a slightly modified version of the code found here : http://code.activestate.com/recipes/577086-heap-sort/

    def HeapSort(A,T):
        def heapify(A):
            start = (len(A) - 2) / 2
            while start >= 0:
                siftDown(A, start, len(A) - 1)
                start -= 1
    
        def siftDown(A, start, end):
            root = start
            while root * 2 + 1 <= end:
                child = root * 2 + 1
                if child + 1 <= end and T.count(A[child]) < T.count(A[child + 1]):
                    child += 1
                if child <= end and T.count(A[root]) < T.count(A[child]):
                    A[root], A[child] = A[child], A[root]
                    root = child
                else:
                    return
    
        heapify(A)
        end = len(A) - 1
        while end > 0:
            A[end], A[0] = A[0], A[end]
            siftDown(A, 0, end - 1)
            end -= 1
    
    
    if __name__ == '__main__':
        text = "the quick brown fox jumped over the the quick brown quick log log"
        heap = list(set(text.split()))
        print heap
    
        HeapSort(heap,text)
        print heap
    

    Output

    ['brown', 'log', 'jumped', 'over', 'fox', 'quick', 'the']
    ['jumped', 'fox', 'over', 'brown', 'log', 'the', 'quick']
    

    you can visualize the program here http://goo.gl/2a9Bh

提交回复
热议问题