def test_measure_time_of_all_algs2(): results = [] results.append(test_alg_2(lambda array: quick_sort(array, 0, 10000 - 1, sort_in_ascending, pivot_on_zero))) print(results) # results.append({'Name': 'Quick Sort', 'value': time_algorithm(quick_sort, 0, 10000 - 1, sort_in_ascending, pivot_on_zero)}) # results.append({'Name': 'Merge Sort', 'value': time_algorithm(merge_sort, sort_in_ascending)}) # results.append({'Name': 'Insertion Sort', 'value': time_algorithm(insertion_sort, sort_in_ascending)}) # results.append({'Name': 'Selection Sort', 'value': time_algorithm(selection_sort, sort_in_ascending)}) # results.append({'Name': 'Bubble Sort', 'value': time_algorithm(bubble_sort, sort_in_ascending)}) # results.append({'Name': 'Python Internal Sort', 'value': time_algorithm(sorted)}) # sortedResults = sorted(results, key=itemgetter('value')) # print('***********************') # for result in sortedResults: # print(result['Name'] + ': ' + str(result['value'])) # print('***********************')
def test_quick_sort_sorts_array_of_numbers_with_zero_pivot(): array = [1, 5, 3, 2, 9, 7, 4] quick_sort(array, 0, 6, sort_in_ascending, pivot_on_zero) assert array == [1, 2, 3, 4, 5, 7, 9]
def test_quick_sort_has_right_number_of_comparisons(): track = {'comparisons': 0, 'copies': 0} array = parse_text(text) quick_sort(array, 0, len(array) - 1, sort_ascending_while_tracking, pivot_on_zero, track) assert track['comparisons'] == 171705 assert track['copies'] == 113769
def test_quick_sort_sorts_the_challenge_list(): array = parse_text(text) quick_sort(array, 0, len(array) - 1, sort_in_ascending, pivot_on_zero) assert array[:20] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] assert array[-20:] == [9981, 9982, 9983, 9984, 9985, 9986, 9987, 9988, 9989, 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 10000]
def test_quick_sort_sorts_array_of_numbers_Descending_with_random_pivot(): array = [1, 5, 3, 2, 9, 7, 4] quick_sort(array, 0, 6, sort_in_descending, pivot_on_high) assert array == [9, 7, 5, 4, 3, 2, 1]
def test_quick_sort_sorts_array_of_numbers_with_random_pivot(): array = [1, 5, 3, 2, 9, 7, 4] quick_sort(array, 0, len(array) - 1, sort_in_ascending, pivot_on_random) assert array == [1, 2, 3, 4, 5, 7, 9]
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()
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)