コード例 #1
0
def test_merge_sort_sorts_the_challenge_list():
    array = parse_text(text)
    result = merge_sort(array, sort_in_ascending)
    assert result[:20] == [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ]
    assert result[-20:] == [
        9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992,
        9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000
    ]
コード例 #2
0
def test_merge_sort_has_right_number_of_comparisons():
    track = {'comparisons': 0, 'copies': 0}
    array = parse_text(text)
    result = merge_sort(array, sort_ascending_while_tracking, track)
    assert result[:20] == [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ]
    assert result[-20:] == [
        9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992,
        9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000
    ]
    assert track['comparisons'] == 120473
    assert track['copies'] == 133616
コード例 #3
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()
コード例 #4
0
def test_merge_sort_performs_many_operation_for_complex_unsorted_list():
    track = {'comparisons': 0, 'copies': 0}
    array = [6, 3, 9, 4, 1, 10, 11, 7, 2, 8]
    merge_sort(array, sort_ascending_while_tracking, track)
    assert track['comparisons'] == 23
コード例 #5
0
def test_merge_sort_returns_array_of_1():
    array = [5]
    result = merge_sort(array, sort_in_ascending)
    assert result == array
コード例 #6
0
def test_merge_sort_performs_three_operation_for_2_item_unsorted_list():
    track = {'comparisons': 0, 'copies': 0}
    array = [2, 4, 1]
    merge_sort(array, sort_ascending_while_tracking, track)
    assert track['comparisons'] == 3
コード例 #7
0
def test_merge_sort_sorts_a_short_list_of_strings_descending():
    array = ['lizard', 'cat', 'snake', 'dog']
    result = merge_sort(array, sort_in_descending)
    assert result == ['snake', 'lizard', 'dog', 'cat']
コード例 #8
0
def test_merge_sort_sorts_a_short_list_descending():
    array = [1, 5, 3, 2, 9, 7, 4]
    result = merge_sort(array, sort_in_descending)
    assert result == [9, 7, 5, 4, 3, 2, 1]
コード例 #9
0
def test_merge_sort_sorts_a_short_list_ascending():
    array = [1, 5, 3, 2, 9, 7, 4]
    result = merge_sort(array, sort_in_ascending)
    assert result == [1, 2, 3, 4, 5, 7, 9]
コード例 #10
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)