Пример #1
0
def test_sort_5():
    A = random_array(5)
    Acopy = A[:]
    Acopy.sort()
    heap.heapsort(A)
    for x in range(0, len(Acopy)):
        assert A[x] == Acopy[x]
Пример #2
0
def time_execute_heapsort_v100():
    before = time.time()
    heapsort(v100)
    after = time.time()
    total = ((after - before) * 1000)

    print("time heapsort = (100 elements) %0.4f" % total)
    def note(self):
        '''
        Summary
        =
        Print chapter6.4 note

        Example
        =
        >>> Chapter6_4().note()
        '''
        print('6.4 堆排序算法')
        print('设n = len(A)-1,堆排序算法先用BuildMaxHeap将输入数组 A[0..n]构造成一个最大堆')
        print('因为数组中的最大元素在根A[0],则可以通过把它与A[n]互换来达到最终正确的位置')
        print('现在如果从堆中去掉结点n(通过减小heapsize[A]),可以很容易地将A[1..n-1]建成最大堆,',
              '原来根的子女仍然是最大堆,而新的元素可能违背了最大堆的性质,这时调用MaxHeapify(A, 0)就可以保持这一个性质')
        print('堆排序算法不断重复这个过程,堆的大小由n-1一直降到2')
        print('堆排序算法的一个举例[7, 6, 5, 4, 3, 2, 1]',
              heap.heapsort([1, 2, 3, 4, 5, 6, 7]))
        print('HeapSort过程的时间代价O(nlgn)')
        print(
            '调用heap.buildmaxheap的时间为O(n),n-1次heap.maxheapify中每一次的时间代价为O(lgn)')
        A = [5, 13, 2, 25, 7, 17, 20, 8, 4]
        print('练习6.4-1 数组', _deepcopy(A), '的heapsort过程结果为:', heap.heapsort(A))
        print('练习6.4-2 证明循环不变式的过程略')
        print('练习6.4-3 按递增排序的数组A已经是一个最大堆,buildmaxheap的时间较少,但是交换元素花费时间较多')
        print(' 若A的元素按降序排列,则buildmaxheap的花费时间较多,元素交换时间差不多')
        print('练习6.4-4 略')
        print('练习6.4-5 略')
        # python src/chapter6/chapter6_4.py
        # python3 src/chapter6/chapter6_4.py
        return self
  def test_heap_sort_correctness(self):
    length = randint(100, 1000)
    input = gen_random_input(length, 1000)
    output = heapsort(input)

    self.assertEqual(len(output), length)
    self.assertTrue(is_sorted(output))
Пример #5
0
def createYoungTab(t):
    for r in t:
        build_max_heap(r)
        heapsort(r)

    nr = len(t[0])  #no of columns
    nc = len(t)

    a = []
    for j in range(nc):
        cs = []
        for i in range(nr):
            cs.append(t[i][j])
        heapsort(cs)

    a_n = np.array(a)
    print(a_n)
    print("=-")
Пример #6
0
print("\nTesting Heap\n")

#L = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
L = [5, 14, 10, 8, 7, 9, 3, 2, 4, 1]

print("Initial L = ", L)

H = heap.Heap(L)

print("Heaped L = ", H)

H.build_heap()

print("Heaped L (should be equal)= ", H)

L = heap.heapsort(L)

print("Sorted L = ", L)

#### Priority queue test

print("\nTesting Priority queue\n")

S = Priority_queue([9, 8, 5, 6, 7, 1, 0, 4, 1, 3])
print("Original S: ", S)

M = S.extract_max()
print("Maximum extracted: ", M)
print("S after maximum extracted: ", S, "\n")

S = Priority_queue([12, 8, 5, 6, 7, 1, 0, 4, 1, 3])
Пример #7
0
        title = "Insertion Sort"
        generator = insertion_sort(vector)
    elif input_is == "b":
        title = "Bucket Sort"
        generator = bucketsort(vector, bucketSize=DEFAULT_BUCKET_SIZE)
    elif input_is == "m":
        title = "Merge Sort"
        generator = mergesort(vector, 0, number_interation - 1)
    elif input_is == "s":
        title = "Shell Sort"
        generator = shellsort(vector)
    elif input_is == "q":
        title = "Quick Sort"
        generator = quicksort_graph(vector, 0, number_interation - 1)
    elif input_is == "h":
        generator = heapsort(vector)
        title = 'heapsort'
    elif input_is == "r":
        generator = radixsort(vector)
        title = 'radix'

    # Inicializando grafico
    fig, ax = plt.subplots()
    ax.set_title(title)

    #bar

    bar = ax.bar(range(len(vector)), vector, align="edge")

    ax.set_xlim(0, number_interation)
    ax.set_ylim(0, int(1.07 * number_interation))