def start_algorithm(): """ Starts the algorithm based on the selection from the user in the ComboBox Expected Complexity: dependent on the sort selected """ global data_list if algorithm_menu.get() == "Bubble Sort": bubble_sort(data_list, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Selection Sort": selection_sort(data_list, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Insertion Sort": insertion_sort(data_list, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Quick Sort": quick_sort(data_list, 0, len(data_list) - 1, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Merge Sort": merge_sort(data_list, 0, len(data_list) - 1, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Heap Sort": heap_sort(data_list, draw_data, speed_scale.get()) elif algorithm_menu.get() == "Cocktail Sort": cocktail_sort(data_list, draw_data, speed_scale.get())
def test_merge_sort(self): for x in range(TEST_ARRAYS_COUNT): array = [random.randrange(0, 1000) for y in range(x * 100 + 1)] unsorted = array.copy() unsorted.sort() merge_sort(array) self.assertEqual(unsorted, array)
def test2(self): arr = [1, 2, 3, 4, 5, 6, 7] arr.reverse() merge_sort(0, len(arr) - 1, arr) assert arr == [1, 2, 3, 4, 5, 6, 7]
def test(self): case_1 = [6, 5, 4, 3, 2, 1] case_2 = [2, 3, 1, 5, 6, 4] merge_sort(case_1) merge_sort(case_2) self.assertEqual([1, 2, 3, 4, 5, 6], case_1) self.assertEqual([1, 2, 3, 4, 5], case_2)
def StartAlgorithm(): global data if not data: return if algo_menu.get() == "Bubble Sort": bubble_sort(data, drawData, speedScale.get()) elif algo_menu.get() == "Merge Sort": merge_sort(data, drawData, speedScale.get()) drawData(data, ["skyblue" for x in range(len(data))])
def start_algo(): global data if not data: return if algo_menu.get() == 'Quick Sort': quick_sort(data, 0, len(data) - 1, draw_data, speed_scale.get()) elif algo_menu.get() == 'Bubble Sort': bubble_sort(data, draw_data, speed_scale.get()) elif algo_menu.get() == 'Merge Sort': merge_sort(data, draw_data, speed_scale.get()) draw_data(data, ['purple' for i in range(len(data))])
def StartAlgorithm(): global data if not data: return if select_algortiham.get() == 'Selection Sort': selection_sort(data, drawData, speedScale.get()) elif select_algortiham.get() == 'Bubble Sort': bubble_sort(data, drawData, speedScale.get()) elif select_algortiham.get() == 'Merge Sort': merge_sort(data, drawData, speedScale.get())
def test_merge_sort_list(self): l = List() l.append(ListItem("d")) l.append(ListItem("a")) self.assertEqual(merge_sort(l).get_items(), ["a", "d"]) l = List() l.append(ListItem("d")) l.append(ListItem("a")) l.append(ListItem("v")) l.append(ListItem("b")) l.append(ListItem("z")) self.assertEqual(merge_sort(l).get_items(), ["a", "b", "d", "v", "z"])
def StartAlgorithm(): global data if not data: return if algMenu.get() == 'Quick Sort': quick_sort(data, 0, len(data)-1, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))]) elif algMenu.get() == 'Bubble Sort': bubble_sort(data, drawData, speedScale.get()) elif algMenu.get() == 'Merge Sort': merge_sort(data, drawData, speedScale.get()) drawData(data, ['green' for x in range(len(data))])
def test_merge(user_int): test_list = [i for i in range(user_int)] random.shuffle(test_list) start_time = time.time() merge_sort(test_list) final_time = time.time() total_time = final_time - start_time result_str = generate_results(test_list, total_time, " Merge") print(result_str) return None
def find_sum(data, target): data = merge_sort(data) for i in range(0, len(data)): index = binary_search(data, target - data[i]) if target - data[i] > 0 and index != -1: return data[i], data[index] return -1, -1
def test_merge(user_int): # build the test list test_list = [i for i in range(user_int)] random.shuffle(test_list) # time tracking of the sort start_time = time.time() merge_sort(test_list) final_time = time.time() # generate and print results total_time = final_time - start_time result_str = generate_results(test_list, total_time, " Merge") print(result_str) return None
def run_sort(n): output = [0, 0, 0, 0] # Generate strings (a, b) = generate_alphanumeric_strings(n, STRING_WIDTH) # Insertion sort if n <= 1000: print("Running insertion sort...") insertion_a = a.copy() st = time.time() insertion_sorted = insertion_sort(insertion_a) output[0] = time.time() - st print("Sort took a total of %s seconds" % str(time.time() - st)) verify_sort(insertion_sorted) print("\n") else: print( "Insertion sort only runs up to a value of 100000 otherwise it takes too long." ) print("\n") # Quick sort print("Running quick sort...") quick_a = a.copy() st = time.time() quick_sorted = quick_sort(quick_a, 0, len(quick_a) - 1) output[1] = time.time() - st print("Sort took a total of %s seconds" % str(time.time() - st)) verify_sort(quick_sorted) print("\n") if n <= 1000000: # Merge sort print("Running merge sort...") merge_a = a.copy() st = time.time() merge_sorted = merge_sort(merge_a) output[2] = time.time() - st print("Sort took a total of %s seconds" % str(time.time() - st)) verify_sort(merge_sorted) print("\n") if n <= 100000: # Tree hash sort print("Running tree hash sort...") hash_a = a.copy() st = time.time() t = HashTable(values=hash_a, accuracy=ACCURACY) t.sort() output[3] = time.time() - st print("Sort took a total of %s seconds" % str(time.time() - st)) hash_sorted = t.get_values() verify_sort(hash_sorted) print("\n") return output
def start_algorithm(): global data selected_algo = algo_list.get("anchor") # print("Selected Algorithm "+selectedAlgo) # print(data) # bubble_sort(data, draw_data, speed_scale.get()) if not data: return if selected_algo == 'Bubble Sort': bubble_sort(data, draw_data, speed_scale.get()) # elif selected_algo == 'Quick Sort': # quick_sort(data, 0, len(data)-1, drawData, speedScale.get()) # drawData(data, ['green' for x in range(len(data))]) # elif selected_algo == 'Merge Sort': merge_sort(data, draw_data, speed_scale.get())
def _sort_list(self, l): """ Sorts a leaf node (list). Args: l (List): list of items Returns: list: sorted list of items """ return merge_sort(l)
def generateSchedules(scheduleList, classList): i = 0 while i < len(scheduleList): j = i + 1 while j < len(scheduleList): if checkLists(scheduleList[i], scheduleList[j], classList) == 1: scheduleList.append( merge_sort(scheduleList[i] + scheduleList[j])) j += 1 i += 1 return removeDuplicates(scheduleList)
def merge_sort_time(): #Create empty dictionary execution_time = {} for i in range(500, 1001, 500): try: unsorted_array = (random.sample(range(i), i)) except ValueError: print("Sample larger than population or is negative") start_time = time.time() merge_sort(unsorted_array, 0, len(unsorted_array)) end_time = time.time() #Input size and its corresponding execution time is added to dictionary execution_time[i] = end_time - start_time print(execution_time) plot_sorting_algorithm(execution_time)
def test_merge_sort(self): array_1 = [1, 5, 3, 2, 4] merge_sort(array_1, 0, len(array_1)) self.assertListEqual(array_1, [1, 2, 3, 4, 5]) array_2 = [1, 6, 5, 2, 4] merge_sort(array_2, 0, len(array_2)) self.assertListEqual(array_2, [1, 2, 4, 5, 6]) array_3 = [1, 6, 5, 2, 10, 4] merge_sort(array_3, 0, len(array_3)) self.assertListEqual(array_3, [1, 2, 4, 5, 6, 10]) array_4 = [1, 5, 3, 2, 4, 5, 6, 25] merge_sort(array_4, 0, len(array_4)) self.assertListEqual(array_4, [1, 2, 3, 4, 5, 5, 6, 25])
conn.close() array_received = eval(arraystring) final_array = merge(array_received,array_pi) time_taken = time.time() - start_time #Calculates and records time_taken print('Time taken for multi core sort', time_taken, 'seconds.') check_sort = time.time() sorted_here = merge_sort(array) #evaluating if it's correct by running the sort on a single core print("Sort truth evaluation: ",sorted_here == final_array) single_core = time.time() - check_sort print("time taken for single core operation: ",single_core) file = open('test_results.txt','a') file.write("/n") file.write("length of list is: %d ,single core time is: %f ,multi core time is: %f " % (l,single_core,time_taken)) file.write("/n") file.write("time gain : %f" % (single_core/time_taken)) file.close()
# alg é o algoritmo escolhido alg = 0 while alg == 1 or 2 or 3 or 4: alg = int( input( 'Qual algoritmo você deseja utilizar?\n1 - Insert-Sort\n2 - Merge-Sort\n3 - Selection-Sort\n' '4 - Bubble-Sort\n5 - Shell-Sort\n6 - Cocktail-Sort')) if alg == 1: comeco = time.time() insert_sort(numeros) fim = time.time() - comeco alg = 'Insert-Sort' break elif alg == 2: comeco = time.time() merge_sort(numeros, 0, len(numeros) - 1) fim = time.time() - comeco alg = 'Merge-Sort' break elif alg == 3: comeco = time.time() selection_sort(numeros) fim = time.time() - comeco alg = 'Selection-Sort' break elif alg == 4: comeco = time.time() bubble_sort(numeros) fim = time.time() - comeco alg = 'Bubble-Sort' break
def test3(self): arr = [5, 4, 1, 6, 8, 10, 2, 4, 7] merge_sort(0, len(arr) - 1, arr) assert arr == [1, 2, 4, 4, 5, 6, 7, 8, 10]
"{0:.3f}".format(time.time() - start_time)) generate_outputfile(numbers, "selection_sort", filename.split("in")[0]) print( "---------------------------------------------------------------" ) time.sleep(2) elif option == '3': # Merge Sort numbers, filename = process_inputfile() print("\nMerge Sort running...") start_time = time.time() # get initial time merge_sort(numbers) print("\n> Merge Sort finished with sorting time: %s seconds." % "{0:.3f}".format(time.time() - start_time)) generate_outputfile(numbers, "merge_sort", filename.split("in")[0]) print( "---------------------------------------------------------------" ) time.sleep(2) elif option == '4': # Quick Sort numbers, filename = process_inputfile() print("\nQuick Sort running...") start_time = time.time() # get initial time
def test1(self): arr = [1, 2, 3, 4, 5, 6, 7] merge_sort(0, len(arr) - 1, arr) assert arr == [1, 2, 3, 4, 5, 6, 7]
def test_merge_sort(self): foo = list(test_list) merge_sort(foo) self.assertEqual(foo, sorted_list)
from Merge import merge from MergeSort import merge_sort # Tests for merge routine assert(merge([], []) == []) assert(merge([1], []) == [1]) assert(merge([], [1]) == [1]) assert(merge([1], [1]) == [1,1]) assert(merge([1,2], [3]) == [1,2,3]) assert(merge([3], [1,2]) == [1,2,3]) assert(merge([1,2,3,4,4,5,6,7,7,8,9], [1,1,5,5,5,5,5,6,6,9]) == [1,1,1,2,3,4,4,5,5,5,5,5,5,6,6,6,7,7,8,9,9]) # Tests for merge_sort routine assert(merge_sort([]) == []) assert(merge_sort([5]) == [5]) assert(merge_sort([5,1]) == [1,5]) assert(merge_sort([5,8,1,2,7,9,4,2,1,5,1]) == [1,1,1,2,2,4,5,5,7,8,9])
def merge_sort_multi(list_part): manager_list.append(merge_sort(list_part))
def test_merge_sort_int(self): self.assertEqual(merge_sort(self.int_list.get_items()), [0, 0, 2, 3, 8, 9, 15, 60, 212, 300])
from QuickSort import quick_sort from MergeSort import merge_sort array = [10, 21, 3, 4, 25, 213, 32, 4, 15, 21] print("Unsorted array") for i in array: print(i, end=" ") qs_array = array.copy() # performs shallow copy quick_sort(qs_array) print("\n======== Quick sort ========") for i in qs_array: print(i, end=" ") ms_array = array.copy() merge_sort(ms_array) print("\n======== Merge sort ========") for i in ms_array: print(i, end=" ")
def test_merge_sort_str(self): self.assertEqual( merge_sort(self.str_list.get_items()), ["a", "a", "b", "c", "e", "g", "p", "q", "v", "z"], )