Beispiel #1
0
def test_bubble_sort_sorts_parsed_text_file_with_odd_event_comparator():
    parsed_text = parse_txt_file(text)
    bubble_sort(parsed_text, odd_even_comparator)
    first_ten = parsed_text[:10]
    last_ten = parsed_text[-10:]
    assert first_ten == [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
    assert last_ten == [9982, 9984, 9986, 9988, 9990, 9992, 9994, 9996, 9998, 10000]
Beispiel #2
0
def test_bubble_sort_has_right_number_of_comparisons():
    track = {'comparisons': 0, "copies": 0}
    array = parse_txt_file(lotsa_numbers)
    bubble_sort(array, sort_ascending_while_tracking, track)
    assert track['comparisons'] == 49988559
    assert track['copies'] == 71844390
Beispiel #3
0
def test_bubble_sort_sorts_with_odds_ascending_evens_descending():
    array = [3, 4, 8, 6, 9, 1]
    expected = [1, 3, 9, 4, 6, 8]
    bubble_sort(array, odd_even_comparator)
    assert array == expected
Beispiel #4
0
def test_bubble_sort_sorts_a_random_list_of_strings():
    array = ["bird", "cat", "dog", "fish", "octopus", "snake"]
    expected = ["bird", "cat", "dog", "fish", "octopus", "snake"]
    random.shuffle(array)
    bubble_sort(array, sort_in_ascending)
    assert array == expected
Beispiel #5
0
def test_bubble_sort_sorts_array_of_strings():
    array = ["cat", "dog", "bird", "fish", "snake"]
    bubble_sort(array, sort_in_ascending)
    assert array == ["bird", "cat", "dog", "fish", "snake"]
Beispiel #6
0
def test_bubble_sort_sorts_a_random_list_of_numbers():
    array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    random.shuffle(array)
    bubble_sort(array, sort_in_ascending)
    assert array == expected
Beispiel #7
0
def test_bubble_sort_sorts_array_of_numbers():
    array = [1, 5, 3, 2, 7]
    bubble_sort(array, sort_in_ascending)
    assert array == [1, 2, 3, 5, 7]
Beispiel #8
0
def Start_alg():
    global data, size, crono

    if alg_menu.get() == "Bubble Sort":  # if buble sort selected:
        start = time.perf_counter()  # start a timer
        bubble_sort(data, drawdata, 0)  # call the sort function
        end = time.perf_counter()  # stop the timer when the function ends
        timetext = str(f'Bubble {size} en {round(end - start, 2)} \n'
                       )  # write the timing in the program
        crono.insert(0.0, str(timetext))
        dbb_alg = "Bubble"  # create variables to insert them in the SQL table
        dbb_size = size
        dbb_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbb_alg, dbb_size, dbb_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()  # insert the data

    elif alg_menu.get() == "Quick Sort":
        start = time.perf_counter()
        quick_sort(data, 0, len(data) - 1, drawdata, 0)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        timetext = str(f'Quick {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbq_alg = "quick"
        dbq_size = size
        dbq_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbq_alg, dbq_size, dbq_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Insertion Sort":
        start = time.perf_counter()
        insertion(data, drawdata, 0)
        end = time.perf_counter()
        timetext = str(f'Insertion {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbi_alg = "insertion"
        dbi_size = size
        dbi_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbi_alg, dbi_size, dbi_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Merge Sort":
        start = time.perf_counter()
        merge_sort(data, drawdata, 0)
        end = time.perf_counter()
        timetext = str(f'Merge {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbm_alg = "merge"
        dbm_size = size
        dbm_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbm_alg, dbm_size, dbm_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Selection Sort":
        start = time.perf_counter()
        selection(data, drawdata, 0)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        timetext = str(f'Selection {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbm_alg = "selection"
        dbm_size = size
        dbm_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbm_alg, dbm_size, dbm_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Opti Bubble Sort":
        start = time.perf_counter()
        opti_bubble(data, drawdata, 0)
        end = time.perf_counter()
        timetext = str(f'Opti_Bubble {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbb_alg = "opti_bubble"
        dbb_size = size
        dbb_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbb_alg, dbb_size, dbb_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Random Sort":
        start = time.perf_counter()
        random_sorts(data, drawdata, 0)
        end = time.perf_counter()
        timetext = str(
            f'Random {size} en {round(end - start, 2)} i {len(trys)} intents \n'
        )
        crono.insert(0.0, str(timetext))
        dbr_alg = "random"
        dbr_size = size
        dbr_sec = round(end - start, 2)
        dbr_trys = len(trys)
        sqlformula = "INSERT INTO sortdata (alg, size, sec, trys) VALUES (%s, %s, %s, %s)"
        dades = (dbr_alg, dbr_size, dbr_sec, dbr_trys)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Shell Sort":
        start = time.perf_counter()
        shell(data, drawdata, 0)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        timetext = str(f'Shell {size} en {round(end - start, 2)} \n')
        crono.insert(0.0, str(timetext))
        dbs_alg = "Shell"
        dbs_size = size
        dbs_sec = round(end - start, 2)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbs_alg, dbs_size, dbs_sec)
        mycursor.execute(sqlformula, dades)
        mydb.commit()

    elif alg_menu.get() == "Counting Sort":
        start = time.perf_counter()
        counting(data, drawdata, 0)
        end = time.perf_counter()
        timetext = str(f'Counting {size} en {round(end - start, 4)} \n')
        crono.insert(0.0, str(timetext))
        dbs_alg = "Counting"
        dbs_size = size
        dbs_sec = round(end - start, 4)
        sqlformula = "INSERT INTO sortdata (alg, size, sec) VALUES (%s, %s, %s)"
        dades = (dbs_alg, dbs_size, dbs_sec)
        mycursor.execute(sqlformula, dades)

    mycursor.execute('delete from sortdata where size=0')
    mydb.commit()
Beispiel #9
0
def Start_alg():
    global data, size, crono, speed_entry

    if checkvar.get() == 1:
        menu.iconify()
        time.sleep(1)

    try:
        speed = float(speed_entry.get())
    except:
        speed = 0

    if alg_menu.get() == "Bubble Sort":  # if buble sort selected:
        start = time.perf_counter()  # start a timer
        bubble_sort(data, drawdata, speed)  # call the sort function
        end = time.perf_counter()  # stop the timer when the function ends
        sorting_algs_func('bubble', end, start, size, speed)

    elif alg_menu.get() == "Quick Sort":
        start = time.perf_counter()
        quick_sort(data, 0, len(data) - 1, drawdata, speed)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        sorting_algs_func('quick', end, start, size, speed)

    elif alg_menu.get() == "Insertion Sort":
        start = time.perf_counter()
        insertion(data, drawdata, speed)
        end = time.perf_counter()
        sorting_algs_func('insertion', end, start, size, speed)

    elif alg_menu.get() == "Merge Sort":
        start = time.perf_counter()
        merge_sort(data, drawdata, speed)
        end = time.perf_counter()
        sorting_algs_func('merge', end, start, size, speed)

    elif alg_menu.get() == "Selection Sort":
        start = time.perf_counter()
        selection(data, drawdata, speed)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        sorting_algs_func('selection', end, start, size, speed)

    elif alg_menu.get() == "Opti Bubble Sort":
        start = time.perf_counter()
        opti_bubble(data, drawdata, speed)
        end = time.perf_counter()
        sorting_algs_func('optibubble', end, start, size, speed)

    elif alg_menu.get() == "Random Sort":
        start = time.perf_counter()
        random_sorts(data, drawdata, speed)
        end = time.perf_counter()
        sorting_algs_func('bogo', end, start, size, speed)

    elif alg_menu.get() == "Shell Sort":
        start = time.perf_counter()
        shell(data, drawdata, speed)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        sorting_algs_func('bogo', end, start, size, speed)

    elif alg_menu.get() == "Counting Sort":
        start = time.perf_counter()
        counting(data, drawdata, speed)
        end = time.perf_counter()
        sorting_algs_func('counting', end, start, size, speed)

    elif alg_menu.get() == "Radix Sort":
        start = time.perf_counter()
        radixSort(data, drawdata, speed)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        sorting_algs_func('radix', end, start, size, speed)

    elif alg_menu.get() == "Cocktail Sort":
        start = time.perf_counter()
        cocktail(data, drawdata, speed)
        end = time.perf_counter()
        drawdata(data, ['green' for x in range(len(data))])
        sorting_algs_func('cocktail', end, start, size, speed)