def test_insertion_sort(self): """ Tests the insertion sort algorithm implementation. """ collection = [5, 1, 10, 2, 16, 1] algorithms.insertion_sort(collection) self.assertEqual(collection, [1, 1, 2, 5, 10, 16]) # test worst case with 10 ints collection = [i for i in xrange(9, -1, -1)] algorithms.bubble_sort(collection) self.assertEqual([i for i in xrange(0, 10)], collection)
def insertion_timer(): # This is the function to time(in seconds) the insertion_sort algorithm. # Filename: 'insertion_timer_datapoints.txt' ip = open("insertion_timer_datapoints.txt", "w") for size in range(1, 201): #for size in range(1, 2): alist = [] for i in range(randint(5000, 15000)): alist.append(randint(1, 1000000)) print("Starting insertion_sort on list #: {}\n".format(size)) start_time = time() insertion_sort(alist) end_time = time() insertion_time = end_time - start_time ip.write("{},{}\n".format(len(alist), insertion_time)) print("Finished insertion_sort'ing list #: {}\n".format(size))
def sort(self): self.draw_bars() self.display_sorttext() if self.algorithm == 'bubble': algorithms.bubble_sort(self) if self.algorithm == 'insertion': algorithms.insertion_sort(self) if self.algorithm == 'quick': algorithms.quick_sort(self, isfirst_call = True) if algorithms.stop_recursive_sort == False: self.issorted = True if self.algorithm == 'merge': algorithms.merge_sort(self, isfirst_call = True) if algorithms.stop_recursive_sort == False: self.issorted = True if self.algorithm == 'heap': algorithms.heap_sort(self) if algorithms.stop_recursive_sort == False: self.issorted = True
def startAlgorithm(): global data tick = speedScale.get() if select_alg.get() == "Bubble sort": sorted_data = bubble_sort(data, draw_data, tick) print(sorted_data) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))]) elif select_alg.get() == "Insertion Sort": sorted_data = insertion_sort(data, draw_data, tick) print(sorted_data) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))]) elif select_alg.get() == "Selection Sort": sorted_data = selection_sort(data, draw_data, tick) print(sorted_data, ) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))]) elif select_alg.get() == "Heap Sort": sorted_data = heap_sort(data, draw_data, tick) print(sorted_data, ) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))]) elif select_alg.get() == "Quick Sort": sorted_data = quick_sort(data, draw_data, tick) print(sorted_data, ) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))]) elif select_alg.get() == "Merge Sort": sorted_data = merge_sort(data, draw_data, tick) print(sorted_data, ) canvas.delete("all") draw_data(sorted_data, ["white" for x in range(len(data))])
def test_multiple_type(self): a = [math.pi, 3.1416, 10, 56, 99, 09.56, -0.9999999999999999, 1] b = insertion_sort(a[:]) self.assertListEqual(sorted(a), b)
def test_float_type(self): a = [10.45, 3.1416, -0.1, -0.5, 1.0, 1.2, 1.200000000000000001, math.pi, -905.34534897349857, 9.5555555555555556, 9.56] b = insertion_sort(a[:]) self.assertListEqual(sorted(a), b)
def test_one_element_array(self): a = [5] b = insertion_sort(a[:]) self.assertListEqual(a, b)
def test_empty_array(self): a = [] b = insertion_sort(a[:]) self.assertListEqual(b, a)
def test_it_sorts_a_inverse_sorted_list_of_numbers(): list_unsorted = [7, 6, 5, 4, 3, 2, 1] insertion_sort(list_unsorted) assert list_unsorted == [1, 2, 3, 4, 5, 6, 7]
print( f"Invalid number was entered, select a number between {min_num} and {max_num}. Try again!" ) ############################################ print("How many integers would you like sorted?: ") array_length = inputNumber(1, 2**64) array = random.sample(range(1, array_length), k=array_length - 1) algorithm_data = { 1: ("Selection Sort", selection_sort(array)), 2: ("Bubble Sort", bubble_sort(array)), 3: ("Insertion Sort", insertion_sort(array)), 4: ("Shell Sort", shell_sort(array)), 5: ("Merge Sort", merge_sort(array)), 6: ("Quick Sort", quick_sort(array, 0, len(array) - 1)), 7: ("Heap Sort", heap_sort(array)) } print("Welcome to Urmzd's Sorting Algorithm Visualizer: ") print("Select one of the following sorting algorithms: ") for k, v in algorithm_data.items(): print(f"Type {k} for {v[0]}") algorithm_number = inputNumber(1, 7) algorithm_name, algorithm = algorithm_data.get(algorithm_number)
def test_empty_array(empty_array): assert insertion_sort(empty_array) == []
def test_identical_array(identical_array): assert insertion_sort(identical_array) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
def call_sort_method(self, random_list, max_vet, j): begin = time.time() insertion_sort(random_list) end = time.time() print('insertion sort with {} items vet[{}]: {} seconds'.format(max_vet, j + 1, end - begin)) return end - begin
def test_it_sorts_a_list_of_numbers(): list_unsorted = [9, 2, 1, 4, 8, 3, 5] insertion_sort(list_unsorted) assert list_unsorted == [1, 2, 3, 4, 5, 8, 9]
def test_it_sorts_a_list_of_numbers_with_duplicates(): list_unsorted = [7, 7, 5, 4, 3, 3, 1] insertion_sort(list_unsorted) assert list_unsorted == [1, 3, 3, 4, 5, 7, 7]
def test_large_array(self): a = [i for i in range(10000, 1, -2)] b = insertion_sort(a[:]) self.assertListEqual(sorted(a), b)
def test_insertion_sort(self): a = [i for i in range(10, 0, -1)] b = insertion_sort(a[:]) self.assertListEqual(sorted(a), b)
def test_descending_array(descending_array): assert insertion_sort(descending_array) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
import file_handler import algorithms import os # CONSTANTS DIRECTORY = os.path.dirname(__file__) FILE_PATH = os.path.join(DIRECTORY, "data.txt") INSERT_OUT_PATH = os.path.join(DIRECTORY, "insert.out") if __name__ == "__main__": data_list = file_handler.read_data_from_file(FILE_PATH) for number_list in data_list: algorithms.insertion_sort(number_list) file_handler.write_data_to_file(data_list, INSERT_OUT_PATH)
def test_in_place_sorting(self): a = [4, 2, 1, 3, 5] insertion_sort(a) self.assertListEqual([1, 2, 3, 4, 5], a)
f'\nComparing algorithms for {len(test_elements)} lists with different lengths:\n' ) for elements in test_elements: list_unsorted = generate_list(elements, MIN_VALUE, MAX_VALUE) print(f'List with {elements} elements:') if elements < THRESHOLD_ELEMENTS_SLOW_ALGORITHMS: bubble_sort(deepcopy(list_unsorted)) print(f'Bubble sort: \t\t{bubble_sort.execution_time}') selection_sort(deepcopy(list_unsorted)) print(f'Selection sort: \t{selection_sort.execution_time}') insertion_sort(deepcopy(list_unsorted)) print(f'Insertion sort: \t{insertion_sort.execution_time}') else: print( 'Bubble sort: \t\tSkipping algorithm because it takes a long time' ) print( 'Selection sort: \tSkipping algorithm because it takes a long time' ) print( 'Insertion sort: \tSkipping algorithm because it takes a long time' ) quick_sort(deepcopy(list_unsorted), 0, len(list_unsorted) - 1) print(f'Quick sort: \t\t{quick_sort.execution_time}')
# print(button) # print(values) sorting_method = button algorithms.delay = 1 if values[len(values) - 1] else 0 # reading information from GUI size = int(values[len(values) - 2]) a = tools.generate_random_list(size) sorting_speed = int(105 - values[len(values) - 3]) if sorting_method == "Selection Sort": generator = algorithms.selection_sort(a) title = "Selection Sort of {} numbers".format(size) elif sorting_method == "Insertion Sort": generator = algorithms.insertion_sort(a) title = "Insertion Sort of {} numbers".format(size) elif sorting_method == "Bubble Sort": generator = algorithms.bubble_sort(a) title = "Bubble Sort of {} numbers".format(size) elif sorting_method == "Merge Sort": generator = algorithms.merge_sort(a) title = "Merge Sort of {} numbers".format(size) # Generating bar graph fig, ax = plt.subplots() ax.set_title(title) bar_rects = ax.bar(range(len(a)), a, align="edge", color="#61b0ff") ax.set_xlim(0, size) ax.set_ylim(0, 100) text = ax.text(0.02, 0.95, "", transform=ax.transAxes)