Exemple #1
0

if __name__ == "__main__":
    import doctest
    doctest.testmod()

    cpt = 0
    tables = int(sys.argv[1])
    i = 1
    file = open("results-first-rand-opt-cpt" + str(tables) + ".dat", "w+")
    while i <= tables:
        file.write(str(100) + " " + str(i) + " ")
        print(100, i, end=" ")
        #First experience with a pivot in the first position
        tab = generate.random_array(
            i
        )  #Create a table whose size is from 1 to 100 (i vraiant de 1 à 100)
        quicksort(
            tab, 0,
            test.cmp)  #Sort with a pivot in the first position (rand = False)
        print(cpt, end=" ")
        file.write(str(cpt) + " ")
        #Second experience with a random pivot
        cpt = 0  #Reset the counter
        tab = generate.random_array(
            i
        )  #Create a table whose size is from 1 to 100 (i vraiant de 1 à 100)
        quicksort(
            tab, 1,
            test.cmp)  #Sort with a pivot in the first position (rand = False)
        print(cpt, end=" ")
Exemple #2
0
    >>> cmp(Element(5),Element(5))
    0
    >>> cmp(Element(5),Element(10))
    -1
    """
    global cpt
    #cpt = cpt + 1
    return Element.cmp(a, b)


if __name__ == "__main__":
    cpt = 0

    import doctest
    doctest.testmod()

    t = generate.random_array(30)
    tt = sorting.merge_sort(t, cmp)
    print(tt)
    if generate.is_sorted(tt):
        print("Yes !!")
    else:
        raise Exception("Array has not been correctly sorted by merge sort")

    print(t)
    sorting.quicksort(t, cmp)
    if generate.is_sorted(t):
        print("Yes !!")
    else:
        raise Exception("Array has not been correctly sorted by quicksort")
    end = 1
else:
    end = input('Input the end value:')
    if array in ('2', '4'):
        count = input('Input the length of the array:')
    if array == '3':
        levels = input('Input the amount of levels:')
        level_length = input('Input the width of a level:')
        level_height = input('Input the height of a level:')
    if array == '4':
        switches = input('Input the amount of switches:')
###################################################

############## Generating the array ###############
if array == '1': unsorted_array = generate.consecutive_array(start, end)
elif array == '2': unsorted_array = generate.random_array(start, end, count)
elif array == '3':
    unsorted_array = generate.few_unique_array(start, levels, level_length,
                                               level_height)
elif array == '4':
    unsorted_array = generate.nearly_sorted_array(start, end, count, switches)
elif array == '5':
    unsorted_array = generate.reversed_array(start, end)
if unsorted_array == -1:
    print('\nAn error occurred while trying to generate an array!')
    print('\nTest finished!')
    exit()
###################################################

################ Sorting the array ################
print('\nSorting in progress...')