def bucketSort(Arr):
    n = len(Arr)
    B = []
    for i in range(0, n):
        B.append([])
    for i in range(0, n):
        B[math.floor(n * Arr[i])].append(Arr[i])
    for i in range(0, n):
        insertion.insertionSort(B[i])
    ArrSort = []
    for i in range(0, n):
        ArrSort += B[i]
    return ArrSort
Beispiel #2
0
    def testInsertionSort(self):

        self.assertEqual(insertionSort([2, 65, 12, 35, 78, 95, 15, 4]),
                         [2, 4, 12, 15, 35, 65, 78, 95])
        self.assertEqual(insertionSort([1, 5, 9, 12, 56, 89, 94, 102]),
                         [1, 5, 9, 12, 56, 89, 94, 102])
        self.assertEqual(insertionSort([2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
        self.assertEqual(insertionSort([]), [])
        self.assertEqual(insertionSort([100, 25]), [25, 100])
        self.assertEqual(insertionSort([2]), [2])
        self.assertEqual(insertionSort([3, 5, 6, 84, 21, 65]),
                         [3, 5, 6, 21, 65, 84])
        self.assertEqual(insertionSort([3, 5, 6, 21, 65]), [3, 5, 6, 21, 65])
import insertion
import mergesort
import linearsearch
import binarysearch

Arr1 = [-3,5,8,7,1,-2,4]
Arr2 = [5,8,-6,12,3,7,1,9,10]
Arr3 = [44,31,-61,12,3,71,1,19,56,-25]

print ("Unsorted array :")
print (Arr1)

print ("Unsorted array :")
print (Arr2)

insertion.insertionSort(Arr1)

print ("Sorted array :")
print (Arr1)

mergesort.mergeSort(Arr2,0,8)

print ("Sorted array :")
print (Arr2)

ind = linearsearch.linear(Arr3,12)

if ind != -1:
   print ("Index of elemet ",ind)
else:
   print ("Element don't exist ")
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 #5
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 #6
0
        print("Array gerado com sucesso\n")

    elif op == 2:
        print("Você escolheu o algoritmo BubbleSort\n")
        array_bub = array.copy()
        inicio = timeit.default_timer()
        result_bub = bubbleSort(array_bub)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 3:
        print("Você escolheu o algoritmo InsertionSort\n")
        array_inser = array.copy()
        inicio = timeit.default_timer()
        result_inser = insertionSort(array_inser)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 4:
        print("Você escolheu o algoritmo MergeSort\n")
        array_merg = array.copy()
        inicio = timeit.default_timer()
        result_merg = mergeSort(array_merg)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 5:
        print("Você escolheu o algoritmo QuickSort\n")