def FirstPlot():
    algo_name = "Selection sort"
    input_data = []
    exec_time = []
    Arr = []
    for n in range(100, 1100, 100):
        Arr = range(n, -1, -1)
        start_time = time.clock()
        selection.selectionSort(Arr)
        end_time = time.clock()
        exec_time.append((end_time - start_time) * 1000)
        input_data.append(n)

    CreatePlot(input_data, exec_time, algo_name)
Beispiel #2
0
    def testSelectionSort(self):

        self.assertEqual(selectionSort([2, 65, 12, 35, 78, 95, 15, 4]),
                         [2, 4, 12, 15, 35, 65, 78, 95])
        self.assertEqual(selectionSort([1, 5, 9, 12, 56, 89, 94, 102]),
                         [1, 5, 9, 12, 56, 89, 94, 102])
        self.assertEqual(selectionSort([2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
        self.assertEqual(selectionSort([]), [])
        self.assertEqual(selectionSort([100, 25]), [25, 100])
        self.assertEqual(selectionSort([2]), [2])
        self.assertEqual(selectionSort([3, 5, 6, 84, 21, 65]),
                         [3, 5, 6, 21, 65, 84])
        self.assertEqual(selectionSort([3, 5, 6, 21, 65]), [3, 5, 6, 21, 65])
for i in range(999,-1,-1):
   testArr2.append(i)   
   
 
testArr3 = []

for i in range(9999,-1,-1):
   testArr3.append(i)    
   

Arr1 = range(0,100)
Arr2 = range(0,1000)
Arr3 = range(0,10000)

selection.selectionSort(testArr1)
selection.selectionSort(testArr2)
selection.selectionSort(testArr3)

pass1 = 1
pass2 = 1
pass3 = 1

for i in range(0,100):
   if testArr1[i] != Arr1[i]:
     pass1 = 0
     break
    
    
for i in range(0,1000):
   if testArr2[i] != Arr2[i]:
    print("---------------------------------------")
    print("1 - Selection Sort")
    print("2 - Bubble Sort")
    print("3 - Shell Sort")
    # print("4 - gera gráfico")
    print("0 - Sair")
    print("---------------------------------------")
    opt = input("Digite uma Opção: ")
    return opt


opt = 0
while opt != '4':
    opt = menu()
    if opt == '1':
        selectionSort(values)
        print(values)
    elif opt == '2':
        bubbleSort(values)
        print(values)
    elif opt == '3':
        shellSort(values)
        print(values)
    # elif opt == '4':
    #     sentinelSearch(vector)
    elif opt == '0':
        print('\n' + 'Obrigado =)' + '\n' + 'Leonardo dos Santos')
        break
    else:
        print('Opção Invalida!')
        break
def main():
    window.fill((255, 255, 255))
    query = [random.randrange(20, 400) for i in range(100)]

    c = [135, 206, 235]
    start = 100
    i = start
    j = 0
    while i < 1000 + start:
        pygame.draw.line(window, c, (i, 500), (i, 500 - query[j]), 5)
        j += 1
        i += 10
        pygame.display.update()

    s = 80
    quick = button(mnt, 10 + s, 520, 180, 50, "quickSort")
    button.draw(quick, window)

    merge = button(mnt, 210 + s, 520, 180, 50, "mergeSort")
    button.draw(merge, window)

    selection = button(mnt, 400 + s, 520, 200, 50, "selectionSort")
    button.draw(selection, window)

    insertion = button(mnt, 610 + s, 520, 200, 50, "insertionSort")
    button.draw(insertion, window)

    bubble = button(mnt, 820 + s, 520, 200, 50, "bubbleSort")
    button.draw(bubble, window)

    speed = Slider("Speed", 0.025, 0.05, 0, 630)

    pygame.display.update()

    pause = button(mnt, 450, 520, 160, 50, "start/stop")

    run = True

    while run:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                pos = pygame.mouse.get_pos()
                if quick.isOver(pos):
                    clearinstruction(window, pause)
                    #message("illustrating ..quickSort",window)
                    quickSort(query, 0, 99, window, pause, speed, 0.025)
                    run = False
                elif merge.isOver(pos):
                    clearinstruction(window, pause)
                    #message("illustrating ..mergeSort",window)
                    mergeSort(query, 0, 99, window, pause, speed)
                    run = False
                elif selection.isOver(pos):
                    clearinstruction(
                        window,
                        pause)  #message("illustrating ..selectionSort",window)
                    selectionSort(query, 0, 99, window, pause, speed)
                    run = False
                elif insertion.isOver(pos):
                    clearinstruction(
                        window,
                        pause)  #message("illustrating ..insertionSort",window)
                    insertionSort(query, 0, 99, window, pause, speed)
                    run = False
                elif bubble.isOver(pos):
                    #message("illustrating ..bubbleSort",window)
                    clearinstruction(window, pause)
                    bubbleSort(query, window, pause, speed)
                    run = False
            elif event.type == pygame.QUIT:
                pygame.quit()

    clearInstruction(window)
    restart = button(mnt, 550, 520, 120, 50, "restart")
    button.draw(restart, window)
    pygame.display.update()

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and restart.isOver(
                    pygame.mouse.get_pos()):
                try:
                    main()
                except:
                    pass

    pygame.quit()
Beispiel #6
0
def plot():
    '''Plottet die Laufzeiten der Sortierverfahren in einem Diagramm '''

    fig = matplotlib.pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)

    # Initialliste
    ogLst = listsGenerator(MAX_NUMBERS_IN_LIST)
    # Kopie fuer Bubblesort
    bubbleLst = copy.deepcopy(ogLst)
    # Kopie fuer InsertionSort
    insertionLst = copy.deepcopy(ogLst)
    # Kopie fuer SelectionSort
    selectionLst = copy.deepcopy(ogLst)
    # Kopie fuer MergeSort
    mergeLst = copy.deepcopy(ogLst)
    # Kopie fuer QuickSort
    quickLst = copy.deepcopy(ogLst)
    quickLst2 = copy.deepcopy(ogLst)

    # Funktion [x ; y]
    # x: Anzahl der Elemente -- y: Zeit zum sortieren
    ax.plot(
        [i for i in range(0, MAX_NUMBERS_IN_LIST)],
        [bubble.bubbleSort(j) for j in bubbleLst],
        '-',
        color='c',
    )
    ax.plot([i for i in range(0, MAX_NUMBERS_IN_LIST)],
            [insertion.insertionSort(j) for j in insertionLst],
            '-',
            color='m')
    ax.plot(
        [i for i in range(0, MAX_NUMBERS_IN_LIST)],
        [selection.selectionSort(j) for j in selectionLst],
        '-',
        color='r',
    )
    ax.plot(
        [i for i in range(0, MAX_NUMBERS_IN_LIST)],
        measureTime(mergeLst),
        '-',
        color='y',
    )
    ax.plot(
        [i for i in range(0, MAX_NUMBERS_IN_LIST)],
        [quick.quickSort(j) for j in quickLst],
        '-',
        color='b',
    )
    # ax.plot([i for i in range(0, MAX_NUMBERS_IN_LIST)], [quick_alt.quickSort(j) for j in quickLst2],  '.')

    # Legende
    ax.set_title("Elemente in Liste im Intervall (0, %d)" % MAX_INTERV)
    ax.legend([
        'BubbleSort', 'InsertionSort', 'SelectionSort', 'MergeSort',
        'QuickSort'
    ])
    ax.set_xlabel('Anzahl Elemente in Liste')
    ax.set_ylabel('Sortierzeit in s')
    matplotlib.pyplot.show()
Beispiel #7
0
    elif op == 5:
        print("Você escolheu o algoritmo QuickSort\n")
        array_quic = array.copy()
        n = len(array_quic)
        inicio = timeit.default_timer()
        result_quic = quickSort(array_quic, 0, n - 1)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 6:
        print("Você escolheu o algoritmo SelectionSort\n")
        array_selec = array.copy()
        inicio = timeit.default_timer()
        result_selec = selectionSort(array_selec)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 7:
        print("Você escolheu o algoritmo ShellSort\n")
        array_shel = array.copy()
        inicio = timeit.default_timer()
        result_shel = shellSort(array_shel)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    else:
        print("Opção Inválida\n")