def test_sort_sorted_list_of_integers(self): """Test that when bubble sort is passed a sorted list of integers that the list remains sorted. """ bubble_sort(self.sorted_list_of_integers) self.assertEqual(self.sorted_list_of_integers, [-7, -2, -1, 0, 1, 4, 10])
def test_sort_unsorted_list_of_integers(self): """Test that when bubble sort is passed an unsorted list of integers that the list is properly sorted. """ bubble_sort(self.unsorted_list_of_integers) self.assertEqual(self.unsorted_list_of_integers, self.sorted_list_of_integers)
def test_bubble_sort_is_stable(self): """Test that if two elements in a list have the same key, that their relative ordering is still preserved after the list is sorted. """ bubble_sort(self.unsorted_list_of_products) self.assertEqual(self.unsorted_list_of_products, self.sorted_list_of_products_stable)
def main(): clip_array = read_file("resources/data.csv") print("\n!------ Bubble sort ------!\n") start = timeit.default_timer() bubble_sort(clip_array.copy()) print("time:", timeit.default_timer() - start, '\n') print(bubble_sort(clip_array)) print("\n!------ Quick sort ------!\n") start = timeit.default_timer() quick_sort(clip_array.copy(), 0, len(clip_array.copy()) - 1) print("time:", timeit.default_timer() - start, '\n') print(quick_sort(clip_array, 0, len(clip_array) - 1))
def test_bubble_sort(self): ints = integer_array_generator(100) self.assertEqual(sorted(ints), bubble_sort(ints)) floats = float_array_generator(100) self.assertEqual(sorted(floats), bubble_sort(floats)) nums = number_array_generator(100) self.assertEqual(sorted(nums), bubble_sort(nums)) chars = char_array_generator(100) self.assertEqual(sorted(chars), bubble_sort(chars)) strings = string_array_generator(100) self.assertEqual(sorted(strings), bubble_sort(strings))
def draw_chart(sort_type, original_data, frame_interval): fig = plt.figure(1, figsize=(16, 9)) data_set = [Data(d) for d in original_data] axs = fig.add_subplot(111) plt.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.95) if sort_type == 1: frames = selection_sort(data_set) elif sort_type == 2: frames = bubble_sort(data_set) elif sort_type == 3: frames = insertion_sort(data_set) elif sort_type == 4: frames = merge_sort(data_set) elif sort_type == 5: frames = quick_sort(data_set) else: raise IndexError def animate(frame_no): # if fi % 2 != 0 and frame_interval < 10 and fi != len(frames)-1: # return bars = [] if len(frames) > frame_no: axs.cla() axs.set_title(s_type[sort_type]) axs.set_xticks([]) axs.set_yticks([]) bars += axs.bar(list(range(Data.length)), [d.value for d in frames[frame_no]], 1, color=[d.color for d in frames[frame_no]]).get_children() frame_no += 1 return bars anim = animation.FuncAnimation(fig, animate, frames=len(frames), interval=frame_interval, repeat=False) return plt, anim
def test_sort_empty_list(self): """Test that when bubble sort is passed an empty list, that nothing happens.""" bubble_sort(self.empty_list) self.assertEqual(self.empty_list, [])
def test_reverse_order(self): # test a list completely out of order x = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] assert bubble_sort.bubble_sort(x) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def test_in_order(self): # test an ordered list x = [1, 2, 3, 4, 5] assert bubble_sort.bubble_sort(x) == [1, 2, 3, 4, 5]
def test_many_items(self): # list with many items and negative items x = [1, 2, 10, 0, 1, -1, 4, 8] assert bubble_sort.bubble_sort(x) == [-1, 0, 1, 1, 2, 4, 8, 10]
def test_bubble_sort_one(self): arr_one: List[int] = [10] bubble_sort.bubble_sort(arr_one) self.assertEqual([10], arr_one)
def test_empty(self): # test empty list x = [] assert bubble_sort.bubble_sort(x) == []
def test_bubble_sort(self): self.assertEqual(bubble_sort.bubble_sort(self.array), self.sorted_array)
def test_bubble_sort(self): random_list = [randint(0, 1000) for _ in xrange(0, 1000)] expected = sorted(random_list) actual = bubble_sort(random_list) self.assertTrue(expected == actual)
def test_bubble_sort_asc(self): expected: List[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] bubble_sort.bubble_sort(arr) self.assertEqual(expected, arr)
def test_bubble_sort_empty(self): arr_one: List[int] = [] bubble_sort.bubble_sort(arr_one) self.assertEqual([], arr_one)
def test_one_item(self): # test one item list x = [1] assert bubble_sort.bubble_sort(x) == [1]
def test_bubble_sort_desc(self): expected: List[int] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] bubble_sort.bubble_sort(arr, True) self.assertEqual(expected, arr)