Beispiel #1
0
"""
if __name__ == '__main__':
    from merge_module import merge_sort
    from quicksort_module import randomized_quicksort, quicksort
    import random

    print("Randomized quick:")
    trues = []
    for i in range(2, 20):
        arr = [random.randint(-50, 150) for x in range(i)]
        randomized_quicksort(arr, 0, len(arr) - 1)
        trues.append(all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1)))

    print("Are all sorted? {}".format(all(trues)))

    print("Regular quick:")
    trues = []
    for i in range(2, 20):
        arr = [random.randint(-50, 150) for x in range(i)]
        quicksort(arr, 0, len(arr) - 1)
        trues.append(all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1)))
    print("Are all sorted? {}".format(all(trues)))

    print("Merge:")
    trues = []
    for i in range(2, 20):
        arr = [random.randint(-50, 150) for x in range(i)]
        merge_sort(arr, 0, len(arr) - 1)
        trues.append(all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1)))
    print("Are all sorted? {}".format(all(trues)))
        test_arr = [
            random.randint(-rand_range, rand_range) for x in range(n[i])
        ]
        start = time.clock()
        randomized_quicksort(test_arr, 0, len(test_arr) - 1)
        end = time.clock()
        times_quick.append(end - start)
        print(n[i])

    times_merge = []
    for i in range(len(n)):
        test_arr = [
            random.randint(-rand_range, rand_range) for x in range(n[i])
        ]
        start = time.clock()
        merge_sort(test_arr, 0, len(test_arr) - 1)
        end = time.clock()
        times_merge.append(end - start)
        print(n[i])

    plt.plot(n, times_quick, 'b', n, times_merge, 'g')
    plt.ylabel('Time')
    plt.xlabel('Number of elements')
    labels = ['uniform distribution - quick']
    labels.append('uniform distribution - merge')
    plt.legend(labels)
    # plt.show()
    figname = 'quick_vs_merge.png'
    plt.savefig(figname)
    print('Plot saved as: ' + figname)
        fig.canvas.manager.window.SetPosition((650 * int(sys.argv[3]), 0))
    else:
        fig.canvas.manager.window.move(650 * int(sys.argv[3]), 0)

    bars = None
    generator = None
    B = [0 for x in range(N)]

    if algo == 'quick':
        generator = quicksort(A, 0, len(A) - 1)
        bars = ax.bar(range(len(A)), A, align='edge', color='red')
    elif algo == 'randomized_quick':
        generator = randomized_quicksort(A, 0, len(A) - 1)
        bars = ax.bar(range(len(A)), A, align='edge', color='orange')
    elif algo == 'merge':
        generator = merge_sort(A, 0, len(A) - 1)
        bars = ax.bar(range(len(A)), A, align='edge', color='magenta')
    elif algo == 'heap':
        generator = heap_sort(A)
        bars = ax.bar(range(len(A)), A, align='edge', color='yellow')
    elif algo == 'bubble':
        generator = bubble_sort(A)
        bars = ax.bar(range(len(A)), A, align='edge', color='cyan')
    elif algo == 'insertion':
        generator = insertion(A)
        bars = ax.bar(range(len(A)), A, align='edge', color='black')
    elif algo == 'bucket':
        generator = bucket_sort(A)
        bars = ax.bar(range(len(A)), A, align='edge', color='brown')
    elif algo == 'counting':
        generator = counting_sort(A, B, N + 1)