def test_radix_not_list_input(): """Test radix sort without a list input.""" from radix import radix_sort with pytest.raises(TypeError) as message: radix_sort("I'm a string") assert "Radix sort only accepts inputs inside a Python list." in str( message)
def test_radix_list_not_ints(): """Test radix sort with a list that isn't full of ints.""" from radix import radix_sort test_list = [7, 4, "taco", "burrito", 5, 9, "horsie"] with pytest.raises(TypeError) as message: radix_sort(test_list) assert "All the items in your list must be integers." in str(message)
def sorting_int_semi_rand(): print("ЧАСТИЧНО УПОРЯДОЧЕННЫЕ ЦЕЛЫЕ ЧИСЛА") for number in ranges: short = np.zeros(number, np.int64) for i in range(len(short)): short[i] = i random.shuffle(short[int(len(short) / 5):int(2 * len(short) / 5)]) random.shuffle(short[int(3 * len(short) / 5):int(4 * len(short) / 5)]) # start = perf_counter_ns() # bubble_sort(short.copy()) # end = perf_counter_ns() # bubble_time = end - start # print("Сортировка пузырьком с размером массива: ", number, " Время: ", bubble_time) start = perf_counter_ns() shell_sort(short.copy()) end = perf_counter_ns() shell_time = end - start print("Сортировка Шелла с размером массива: ", number, " Время: ", shell_time) start = perf_counter_ns() quick_sort(short.copy()) end = perf_counter_ns() quick_time = end - start print("Быстрая сортировка с размером массива: ", number, " Время: ", quick_time) start = perf_counter_ns() merge_sort(short.copy()) end = perf_counter_ns() merge_time = end - start print("Сортировка слиянием с размером массива: ", number, " Время: ", merge_time) start = perf_counter_ns() heap_sort(short.copy()) end = perf_counter_ns() heap_time = end - start print("Сортировка кучей с размером массива: ", number, " Время: ", heap_time) start = perf_counter_ns() radix_sort(short.copy()) end = perf_counter_ns() radix_time = end - start print("Поразрядная сортировка с размером массива: ", number, " Время: ", radix_time) start = perf_counter_ns() sorted(short.copy()) end = perf_counter_ns() sorted_time = end - start print("Встроенная в язык сортировка с размером массива: ", number, " Время: ", sorted_time) return 0
def sorting_short_rand(): print("РАНДОМНЫЕ МАЛЕНЬКИЕ ЧИСЛА") for number in ranges: short = np.zeros(number, np.int64) for i in range(len(short)): short[i] = i % 10 random.shuffle(short) # start = perf_counter_ns() # bubble_sort(short.copy()) # end = perf_counter_ns() # bubble_time = end - start # print("Сортировка пузырьком с размером массива: ", number, " Время: ", bubble_time) start = perf_counter_ns() shell_sort(short.copy()) end = perf_counter_ns() shell_time = end - start print("Сортировка Шелла с размером массива: ", number, " Время: ", shell_time) start = perf_counter_ns() quick_sort(short.copy()) end = perf_counter_ns() quick_time = end - start print("Быстрая сортировка с размером массива: ", number, " Время: ", quick_time) start = perf_counter_ns() merge_sort(short.copy()) end = perf_counter_ns() merge_time = end - start print("Сортировка слиянием с размером массива: ", number, " Время: ", merge_time) start = perf_counter_ns() heap_sort(short.copy()) end = perf_counter_ns() heap_time = end - start print("Сортировка кучей с размером массива: ", number, " Время: ", heap_time) start = perf_counter_ns() radix_sort(short.copy()) end = perf_counter_ns() radix_time = end - start print("Поразрядная сортировка с размером массива: ", number, " Время: ", radix_time) start = perf_counter_ns() sorted(short.copy()) end = perf_counter_ns() sorted_time = end - start print("Встроенная в язык сортировка с размером массива: ", number, " Время: ", sorted_time) return 0
def sorting_int_same_rand(): print("МАССИВ ИЗ ОДИНАКОВЫХ ЦЕЛЫХ ЧИСЕЛ") for number in ranges: short = np.zeros(number, np.int64) for i in range(len(short)): short[i] = 654 # start = perf_counter_ns() # bubble_sort(short.copy()) # end = perf_counter_ns() # bubble_time = end - start # print("Сортировка пузырьком с размером массива: ", number, " Время: ", bubble_time) start = perf_counter_ns() shell_sort(short.copy()) end = perf_counter_ns() shell_time = end - start print("Сортировка Шелла с размером массива: ", number, " Время: ", shell_time) start = perf_counter_ns() quick_sort(short.copy()) end = perf_counter_ns() quick_time = end - start print("Быстрая сортировка с размером массива: ", number, " Время: ", quick_time) start = perf_counter_ns() merge_sort(short.copy()) end = perf_counter_ns() merge_time = end - start print("Сортировка слиянием с размером массива: ", number, " Время: ", merge_time) start = perf_counter_ns() heap_sort(short.copy()) end = perf_counter_ns() heap_time = end - start print("Сортировка кучей с размером массива: ", number, " Время: ", heap_time) start = perf_counter_ns() radix_sort(short.copy()) end = perf_counter_ns() radix_time = end - start print("Поразрядная сортировка с размером массива: ", number, " Время: ", radix_time) start = perf_counter_ns() sorted(short.copy()) end = perf_counter_ns() sorted_time = end - start print("Встроенная в язык сортировка с размером массива: ", number, " Время: ", sorted_time) return 0
def test_radix_sort_sorts_big_list(): """Test that radix sort sorts big list.""" from radix import radix_sort from random import shuffle big_list = list(range(100)) shuffle(big_list) assert radix_sort(big_list) == list(range(100))
def test_radix_sort(self): """ Tests the radix_sort(list) method """ data = [3, 1, 10, 9] results = radix_sort(data) self.assertIsInstance(results, list) self.assertEqual(results, [1, 3, 9, 10]) data = random.sample(range(0, 100), 10) results = radix_sort(data) self.assertEqual(results, sorted(data)) # test empty list data = [] results = radix_sort(data) self.assertIsInstance(results, list) self.assertEqual(results, []) # test single index list data = [1] results = radix_sort(data) self.assertIsInstance(results, list) self.assertEqual(results, [1])
def test_radix_correct_input_long_random(): """Test radix sort with a long, random set of inputs.""" from radix import radix_sort test_list = random.sample(range(10000), random.randrange(100, 10000)) output = test_list[:] assert radix_sort(test_list) == sorted(output)
def test_radix_correct_input_unordered(): """Test radix sort with unordered inputs.""" from radix import radix_sort test_list = [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] output = test_list[:] assert radix_sort(test_list) == sorted(output)
def dc3(input): b_0, b_1, b_2 = [], [], [] for i in xrange(len(input)): if (i % 3) == 0: b_0.append(i) elif (i % 3) == 1: b_1.append(i) elif (i % 3) == 2: b_2.append(i) c = b_1 + b_2 input.extend([0, 0]) # Ensure that each string is a triple r_1 = [tuple(input[x:x+3]) for x in b_1] r_2 = [tuple(input[x:x+3]) for x in b_2] input = input[:-2] r = r_1 + r_2 r_prime = radix.radix_sort(r, max(input)) a_dict = rank(r, r_prime) b_dict = {} tmp = {} counter = 0 for x in c: b_dict[x] = a_dict[counter] tmp[a_dict[counter]] = x counter += 1 b12_sorted_index = [] for i in xrange(1, counter + 1): b12_sorted_index.append(tmp[i]) b_dict[len(input)] = 0 b_dict[len(input) + 1] = 0 b0_dict = {} b0_list = [] max_ = 0 for x in b_0: b0_dict[(input[x], b_dict[x + 1])] = x if input[x] > max_: max_ = input[x] if b_dict[x + 1] > max_: max_ = b_dict[x + 1] b0_list.append((input[x], b_dict[x + 1], 0)) b0_sorted = radix.radix_sort(b0_list, max_) b0_sorted = [x[:-1] for x in b0_sorted] b0_sorted_index = [b0_dict[x] for x in b0_sorted] sorted_array = [] i, j = 0, 0 # Positions in b0, b12 while i < len(b0_sorted_index) and j < len(b12_sorted_index): if b12_sorted_index[j] % 3 == 1: if input[b0_sorted_index[i]] < input[b12_sorted_index[j]]: sorted_array.append(b0_sorted_index[i]) i += 1 elif input[b0_sorted_index[i]] > input[b12_sorted_index[j]]: sorted_array.append(b12_sorted_index[j]) j += 1 else: if b_dict[b0_sorted_index[i] + 1] < b_dict[b12_sorted_index[j] + 1]: sorted_array.append(b0_sorted_index[i]) i += 1 else: sorted_array.append(b12_sorted_index[j]) j += 1 elif b12_sorted_index[j] % 3 == 2: if input[b0_sorted_index[i]] < input[b12_sorted_index[j]]: sorted_array.append(b0_sorted_index[i]) i += 1 elif input[b0_sorted_index[i]] > input[b12_sorted_index[j]]: sorted_array.append(b12_sorted_index[j]) j += 1 else: if input[b0_sorted_index[i] + 1] < input[b12_sorted_index[j] + 1]: sorted_array.append(b0_sorted_index[i]) i += 1 elif input[b0_sorted_index[i] + 1] > input[b12_sorted_index[j] + 1]: sorted_array.append(b12_sorted_index[j]) j += 1 else: if b_dict[b0_sorted_index[i] + 2] < b_dict[b12_sorted_index[j] + 2]: sorted_array.append(b0_sorted_index[i]) i += 1 else: sorted_array.append(b12_sorted_index[j]) j += 1 while i < len(b0_sorted_index): sorted_array.append(b0_sorted_index[i]) i += 1 while j < len(b12_sorted_index): sorted_array.append(b12_sorted_index[j]) j += 1 return sorted_array
def test_partially_sorted(self): arr = [1, 2, 3, 1, 2, 3, 1, 2, 3] res = radix.radix_sort(arr) expected = [1, 1, 1, 2, 2, 2, 3, 3, 3] self.assertFalse(not res) self.assertEqual(expected, res)
def test_reversed(self): arr = [5, 4, 3, 2, 1] res = radix.radix_sort(arr) expected = [1, 2, 3, 4, 5] self.assertFalse(not res) self.assertEqual(expected, res)
def test_radix_sort_n_2_list(): """Test radix sort works on descending value list.""" from radix import radix_sort assert radix_sort([6, 5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5, 6]
def test_radix_sort_with_medium_lists(unsorted_l, sorted_l): """Test radix sort with medium lists.""" from radix import radix_sort assert radix_sort(unsorted_l) == sorted_l
def test_radix_non_int_raises_error(): """Entering an iterable containing non-integers raises an error.""" from radix import radix_sort with pytest.raises(ValueError): radix_sort([1, 2, 3, 5, 'burp'])
def test_radix_sort_returns_ordered_list(input, expected): """Radix sort returns an ordered list.""" from radix import radix_sort assert radix_sort(input) == expected
def test_radix_non_list_raises_error(): """Entering a non-list/tuple param raises an error.""" from radix import radix_sort with pytest.raises(TypeError): radix_sort('Hello')
def test_radix_sort_on_one_item_list(): """Test radix sort with single item list.""" from radix import radix_sort assert radix_sort([5]) == [5]
def test_radix_sort_on_empty_list(): """Test radix sort returns empty list on empty list.""" from radix import radix_sort assert radix_sort([]) == []
def test_radix_no_input(): """Test radix sort with no input.""" from radix import radix_sort with pytest.raises(TypeError): radix_sort()
def test_random_sames(): """Assert same numbers can sort.""" from radix import radix_sort assert radix_sort([2, 2, 2, 1]) == [1, 2, 2, 2]
def test_radix_sort_sorts_random_list(): """Radix sort returns an ordered list.""" from radix import radix_sort input = [randint(0, 1000) for i in range(100)] expected = sorted(input) assert radix_sort(input) == expected
def test_radix_sort_raises_type_error_if_input_not_list(): """Test that radix sort will raise an error if input not a list.""" with pytest.raises(TypeError): from radix import radix_sort radix_sort((1, 2, 4, 3))
def __random_gen(self, n=2 ** 10): for i in range(0, n): self.__random.append(randint(self.__range[0], self.__range[1])) random = radix_sort(self.__random, 32) self.__random = random return random
def test_string(): """Error raised with strings.""" from radix import radix_sort with pytest.raises(TypeError): radix_sort(["hello", 2, 3, 1])
def test_random_lst(lists): """Asser the radix sort works on multiple lists of various sizes.""" from radix import radix_sort for lst in lists: lst_sort = sorted(lst) assert radix_sort(lst) == lst_sort
def test_radix_sort_empty(): """Test that was can pass an empty list.""" from radix import radix_sort assert radix_sort([]) == []
def test_floats(): """Error raised with floats.""" from radix import radix_sort with pytest.raises(TypeError): radix_sort([2.3, 2.8, 2.3, 1])
def test_radix_sort_sorts_small_list(): """Test that radix sort sorts small list.""" from radix import radix_sort assert radix_sort([4, 10, 7, 1, 9]) == [1, 4, 7, 9, 10]
def test_tuple(): """Error raised with tuples.""" from radix import radix_sort with pytest.raises(TypeError): radix_sort([(4, 7), 2, 3, 1])
def test_radix_list_empty(): """Test radix sort throws error with an empty list.""" from radix import radix_sort assert radix_sort([]) == []
def test_empty(self): arr = [] res = radix.radix_sort(arr) expected = [] self.assertEqual(expected, res)
def test_radix_correct_short_input(): """Test radix sort with a short series of inputs.""" from radix import radix_sort test_list = [5, 4, 47, 3, 22] output = test_list[:] assert radix_sort(test_list) == sorted(output)
def test_very_big(self): arr = [random.randint(0, 100) for i in range(10000)] res = radix.radix_sort(arr) arr.sort() self.assertFalse(not res) self.assertEqual(arr, res)
def test_radix_correct_input_already_ordered(): """Test radix sort with an already sorted series of inputs.""" from radix import radix_sort test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] assert radix_sort(test_list) == test_list
def test_trivial(self): arr = [1, 333, 22] res = radix.radix_sort(arr) expected = [1, 22, 333] self.assertFalse(not res) # check res not empty self.assertEqual(expected, res)
def excec_program(list, args, file): list_dup = list.copy() swaps = 0 compares = 0 my_time = 0 print("-------------------") tracemalloc.start() if args[0] == "insert": if file: t1_start = time.process_time() insertion.insertion_sort_stat(list, args[1]) t1_stop = time.process_time() else: t1_start = time.process_time() insertion.insertion_sort(list, args[1]) t1_stop = time.process_time() swaps = insertion.swaps compares = insertion.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "merge": merge.reset_counters() if file: t1_start = time.process_time() merge.merge_sort_stat(list, args[1]) t1_stop = time.process_time() else: t1_start = time.process_time() merge.merge_sort(list, args[1]) t1_stop = time.process_time() swaps = merge.swaps compares = merge.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "quick": quick.reset_counters() if file: t1_start = time.process_time() quick.quick_sort_stat(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() else: t1_start = time.process_time() quick.quick_sort(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() swaps = quick.swaps compares = quick.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "dual_pivot": dual_pivot.reset_counters() if file: t1_start = time.process_time() dual_pivot.dual_sort_stat(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() else: t1_start = time.process_time() dual_pivot.dual_sort(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() swaps = dual_pivot.swaps compares = dual_pivot.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "hybrid": hybrid.reset_counters() if file: t1_start = time.process_time() hybrid.hybrid_sort(list, args[1]) t1_stop = time.process_time() else: t1_start = time.process_time() hybrid.hybrid_sort(list, args[1]) t1_stop = time.process_time() swaps = hybrid.swaps compares = hybrid.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "radix": radix.reset_counters() t1_start = time.process_time() radix.radix_sort(list, args[1]) t1_stop = time.process_time() swaps = radix.swaps compares = '-' my_time = round(t1_stop - t1_start, 8) elif args[0] == "select_dual": select_dual.reset_counters() t1_start = time.process_time() select_dual.dual_sort(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() swaps = select_dual.swaps compares = select_dual.compares my_time = round(t1_stop - t1_start, 8) elif args[0] == "select_quick": select_quick.reset_counters() t1_start = time.process_time() select_quick.quick_sort(list, 0, len(list) - 1, args[1]) t1_stop = time.process_time() swaps = select_quick.swaps compares = select_quick.compares my_time = round(t1_stop - t1_start, 8) mem = tracemalloc.get_traced_memory()[1] tracemalloc.stop() if file: info = str(len(list)) + ';' info += str(swaps) + ';' info += str(compares) + ';' info += str(my_time) + ';' info += str(mem) + ';' info += ('\n') try: file_name = args[2] with open(file_name, 'a+') as f: f.write(info) except FileNotFoundError: print("Creating new file ...") with open(file_name, 'a+') as f: f.write(info) else: print("-----------------") print("Time: %.10f" % (t1_stop - t1_start)) print("Swaps: ", swaps) print("Compares: ", compares) print("Memory: ", mem, "B") sorted_info(list_dup, list) if check_order(list, args[1]) or args[0] == "select_dual": print(list) else: print("Something went wrong :( ")