class BinarySearchTest(unittest.TestCase): def setUp(self): self.BinS = BinarySearch([11, 19, 27, 33, 42, 57, 63, 76, 81, 93, 99 ]) #pass the list while creating the object def tearDown(self): pass def test_binary_search_found(self): self.assertEqual(self.BinS.binary_search(27), 2) #random element self.assertEqual(self.BinS.binary_search(11), 0) #first element self.assertEqual(self.BinS.binary_search(99), 10) #last element def test_binary_search_notFound(self): self.assertEqual(self.BinS.binary_search(53), -1) #test for element which is not in the set
def get_all_solutions(a): solutions = [] sorted_a = sorted(a) n = len(a) for i in range(0, n): j = BinarySearch.binary_search(sorted_a, -a[i]) if j > i: solutions.append((a[i], a[j])) return solutions
def get_all_solutions(a): solutions = [] sorted_a = sorted(a) n = len(a) for i in range(0, n): for j in range(i + 1, n): k = BinarySearch.binary_search(sorted_a, -(a[i] + a[j])) if k > j: solutions.append((a[i], a[j], a[k])) return solutions
def fill_arr(n): list_res = [] for i in range(n): list_res.append(randint(MIN_VALUE, MAX_VALUE)) return list_res array = fill_arr(n) from time import time # Запуск алгоритма from linear_search import LinearSearch linear = LinearSearch(n, array) start_time = time() result_index = linear.linear_search(x) print('Индекс заданного числа в массиве:', result_index, '\n', f'{(time() - start_time) * 1000:.3e}') from binary_search import BinarySearch array.sort() binary = BinarySearch(n, array) start_time = time() result_index = binary.binary_search(x) print('Индекс заданного числа в массиве:', result_index, '\nРезультирующее время:', f'{(time() - start_time) * 1000:.3e}') from binary_search_1 import BinarySearch_1 binary = BinarySearch_1(n, array) start_time = time() result_index = binary.binary_search(x) print('Индекс заданного числа в массиве:', result_index, '\nРезультирующее время:', f'{(time() - start_time) * 1000:.3e}')