def main(): parser = argparse.ArgumentParser(description='Specify type of sorting.') parser.add_argument('-ls', dest='list_size', action='store', type=int, default=10, help='integer for the size of the list to sort.') parser.add_argument('-wt', dest='wait_time', action='store', type=float, default=0, help='integer corresponding to time between prints.') parser.add_argument('-b', dest='algorithm', action='store_const', const=bubble_sort, help='bubble sort', default=None) parser.add_argument('-i', dest='algorithm', action='store_const', const=insertion_sort, help='insertion sort', default=None) parser.add_argument('-bogo', dest='algorithm', action='store_const', const=bogo_sort, help='bogo sort', default=None) parser.add_argument('-q', dest='algorithm', action='store_const', const=quick_sort_all, help='quick sort', default=None) args = parser.parse_args() if args.list_size <= 0: print('specified list size <= 0. Using default list size of 10.') args.list_size = 10 lst = list(range(1, args.list_size + 1)) random.shuffle(lst) if args.algorithm != None: args.algorithm(lst, args.wait_time) else: print('No specified algorithm. Using bubblesort.') bubble_sort(lst, args.wait_time)
def sort(): global data timer = set_sort_speed() if menu.get() == 'bogo_sort': bogo_sort(data, drawBars, timer) elif menu.get() == 'bubble_sort': bubble_sort(data, drawBars, timer) elif menu.get() == 'insertion_sort': insertion_sort(data, drawBars, timer) elif menu.get() == 'quick_sort': quick_sort(data, 0, len(data) - 1, drawBars, timer) elif menu.get() == 'merge_sort': merge_sort(data, 0, len(data) - 1, drawBars, timer)
def visualize_algorithm(self, name: str = 'insertion_sort'): """ Visualizes a specific algorithm """ options = { 'insertion_sort': insertion_sort(self.data), 'bubble_sort': bubble_sort(self.data), 'selection_sort': selection_sort(self.data), 'quick_sort': quick_sort(self.data, 0, len(self.data) - 1), 'merge_sort': merge_sort(self.data, 0, len(self.data)) } algorithm = options[name] plt.title(name) self.ani = animation.FuncAnimation(self.fig, func=self._update_animation, frames=algorithm, interval=1, repeat=False) plt.show()
def test_bubble_sort_positive_numbers(self): self.assertEqual(bubble_sort.bubble_sort([19, 20, 13, 1, 15]), [1, 13, 15, 19, 20])
def test_bubble_sort_already_sorted(self): self.assertEqual(bubble_sort.bubble_sort([1, 2, 3, 4]), [1, 2, 3, 4])
def test_bubble_sort_empty_list(self): self.assertEqual(bubble_sort.bubble_sort([]), [])
def test_bubble_sort_same_numbers(self): self.assertEqual(bubble_sort.bubble_sort([1, 1, 1, 1]), [1, 1, 1, 1])
def test_bubble_sort_negative_numbers(self): self.assertEqual(bubble_sort.bubble_sort([-1, -3, -5, -7, -9, -5]), [-9, -7, -5, -5, -3, -1])