예제 #1
0
def heapSort(arr):
    """
    Sorts an array by creating a heap and continually extracting minimums.
    Array is not sorted in place.
    :type arr: List[]
    :rtype: List[]
    """
    heap = MinHeap()
    heap.createMinHeap(arr)
    return heap.extractAll()
예제 #2
0
def heapFullTest():
    """
    Various tests for Heap class, using both MinHeaps and MaxHeaps, including sorting and random operations.
    """
    print("Testing MinHeap: sorting")
    for i in range(1, 21):
        if heapRandomSort(250, True):
            print "Test", i, "successful"
        else:
            print "Test", i, "failed"

    print("\nTesting MaxHeap: sorting")
    for i in range(1, 21):
        if heapRandomSort(250, False):
            print "Test", i, "successful"
        else:
            print "Test", i, "failed"

    print("\nTesting MinHeap: general")
    for i in range(1, 21):
        if heapRandomTest(250, True):
            print "Test", i, "successful"
        else:
            print "Test", i, "failed"

    print("\nTesting MaxHeap: general")
    for i in range(1, 21):
        if heapRandomTest(250, False):
            print "Test", i, "successful"
        else:
            print "Test", i, "failed"

    print("\nTesting MinHeap: other operations")
    ar = [1, 4, 501, -200, 32, 7, 65, -1, 20000, -34, 17]
    min_heap = MinHeap()
    min_heap.createMinHeap(ar)

    print min_heap.extractMin()
    print min_heap.extractMin()
    print min_heap.extractMin()

    max_heap = MaxHeap()
    max_heap.createMaxHeap(ar)

    print max_heap.extractMax()
    print max_heap.extractMax()
    print max_heap.extractMax()

    print "Max: ar", max(
        ar), "min_heap", min_heap.maximum(), "max_heap", max_heap.maximum()
    print "Min: ar", min(
        ar), "min_heap", min_heap.minimum(), "max_heap", max_heap.minimum()