def insertionSortTime():

    SETUP_CODE = '''
import sort
import random
testList = []
for i in range(50000):
    testList.append(random.randint(0,1000))'''

    TEST_CODE = '''
sort.insertionSort(testList)'''

    times = timeit.repeat(setup=SETUP_CODE, stmt=TEST_CODE, repeat=1, number=1)

    print('insertionSortTime: {}'.format(min(times)))
Exemplo n.º 2
0
    print('2 - Scan Array')
    print('3 - Selection Sort')
    print('4 - Insertion Sort')
    print('5 - Bubble Sort')
    print('6 - Shell Sort')
    print('7 - Quick Sort')
    print('0 - Exit')
    print('==================')


def scanArray():
    for i in range(length):
        array[i] = int(input('Enter the value [' + str(i) + ']: '))
    return array


op = 1
while op:
    printMenu()
    op = int(input('Choose an option: '))

    if op == 0: print('You chose to leave')
    elif op == 1: print(array)
    elif op == 2: print(scanArray())
    elif op == 3: print(sort.selectionSort(array))
    elif op == 4: print(sort.insertionSort(array))
    elif op == 5: print(sort.bubbleSort(array))
    elif op == 6: print(sort.shellSort(array))
    elif op == 7: print(sort.quickSort(array, 0, length - 1))
    else: print('Please choose a valid option')
            print(testList)
    else:
        testList = []
        for i in range(int(sys.argv[2])):
            testList.append(random.randint(0, 10000))
        sort.bubbleSort(testList)
        print("LIST SORTED")

elif sys.argv[1] == "InsertionSort":
    if len(sys.argv) > 3:
        if sys.argv[3] == "PRINT":
            testList = []
            for i in range(int(sys.argv[2])):
                testList.append(random.randint(0, 10000))
            print(testList, "\n")
            sort.insertionSort(testList)
            print(testList)
    else:
        testList = []
        for i in range(int(sys.argv[2])):
            testList.append(random.randint(0, 10000))
        sort.insertionSort(testList)
        print("LIST SORTED")

elif sys.argv[1] == "MergeSort":
    if len(sys.argv) > 3:
        if sys.argv[3] == "PRINT":
            testList = []
            for i in range(int(sys.argv[2])):
                testList.append(random.randint(0, 10000))
            print(testList, "\n")
Exemplo n.º 4
0
import sort
import random

lyst = [1, 3, 2, 4, 7, 6, 5]
print(lyst)
sort.selectionSort(lyst)
print(lyst)
lyst = [1, 3, 2, 4, 7, 6, 5]
print(lyst)
sort.bubbleSort(lyst)
print(lyst)
lyst = [1, 3, 2, 4, 7, 6, 5]
print(lyst)
sort.insertionSort(lyst)
print(lyst)

lyst = []
for count in range(1000):
    lyst.append(random.randint(1, 200))
print(lyst)
sort.quicksort(lyst)
print(lyst)
Exemplo n.º 5
0
    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...')
sorts, times, comparisons, accesses = [], [], [], []
for i in range(tries):
    startTime = time()
    if algorithm == '1':
        sorted_array, comparison, access = sort.someSort(unsorted_array)
    elif algorithm == '2':
        sorted_array, comparison, access = sort.insertionSort(unsorted_array)
    elif algorithm == '3':
        sorted_array, comparison, access = sort.selectionSort(unsorted_array)
    elif algorithm == '4':
        sorted_array, comparison, access = sort.bubbleSort(unsorted_array)
    elif algorithm == '5':
        sorted_array, comparison, access = sort.mergeSort(unsorted_array)
    elif algorithm == '6':
        sorted_array, comparison, access = sort.bogoSort(unsorted_array)
    times.append(time() - startTime)
    sorts.append(sorted(unsorted_array) == sorted_array)
    comparisons.append(comparison)
    accesses.append(access)
###################################################

################ Analyze test data ################
Exemplo n.º 6
0
import random, time


from sort import insertionSort

mylist = [54,26,93,17,77,31,44,55,20]
insertionSort( mylist )
print( mylist )