def test_is_sorted(self): a = int_sort_list[0].copy() merge_sort(a) self.assertEqual(a, int_sort_list[1]) b = str_sort_list[0].copy() merge_sort(b) self.assertEqual(b, str_sort_list[1])
def test_merge_sort(self): a = [5, 8, 1, 9, 6, 3, 10, 7, 2, 4] b = a.copy() b.sort() merge_sort(a, 0, len(a) - 1) self.assertEqual(a, b) c = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] d = c.copy() d.sort() merge_sort(c, 0, len(c) - 1) self.assertEqual(c, d)
def merge_timing(input_size, minimum, maximum): timings = [] for size in input_size: array = gl.generate_list(size, minimum, maximum) time_01 = datetime.now() sorts.merge_sort(array, 0, size - 1) time_02 = datetime.now() time_diff = time_02 - time_01 timings.append(time_diff) return timings
def test__merge_sort(self): for items in self.generate_cases(self.n_cases): ans = sorted(items) t1 = time.time_ns() res = sorts.merge_sort(items) self.ts.append(time.time_ns() - t1) self.assertEqual(res, ans)
def test_merge_sort(self): for test_line in self.test_data: merge_sorted = merge_sort(list(test_line)) tim_sorted = sorted(test_line) self.assertEqual( merge_sorted, tim_sorted, '\n\nFailed to be sorted! The input was:\n{}\n\nmerge_sort returned:\n{}\n\nand it should be:\n{}\n' .format(teal + test_line + NC, red + ''.join(merge_sorted) + NC, green + ''.join(tim_sorted) + NC))
def kruskals(edges): mst = [] graph = make_weighted_graph(edges) forest = [] cost = 0 for vertex in graph.keys(): forest.append(ComponentTree(vertex)) merge_sort(edges) for edge, weight in edges: (v1, v2) = edge # See if the trees have the same root r1, r2 = find_set(forest, v1).root, find_set(forest, v2).root if r1 != r2: cost += weight mst.append(edge) if r1.depth > r2.depth: r1.add_child(r2) forest.remove(r2) else: r2.add_child(r1) forest.remove(r1) return mst, cost
def test_merge_sort(shortlist, longlist, numlist): """ test_merge_sort will test if the merge sort function sorted the given list parameters: 'shortlist' The length of the shortest list to test 'longlist' The length of the longest list to test 'numlist' The number of lists of each length to test eg. test_merge_sort(5, 10, 9) will test merge sort of a list size of 5 through 10 and will test 9 lists of each """ for j in range(shortlist, longlist + 1): print("Testing Length", j) for k in range(numlist): print("List Number ", k + 1) createlist = random_list(j) print("List Unsorted", createlist) sorts.merge_sort(createlist) print("List Sorted ", createlist) if not is_sorted(createlist): assert type(error), "Failed to sort" print("Error Length", j) else: print("Success!")
def test_merge_sort(self): start = timeit.default_timer() Y = sorts.merge_sort(self.random_seq) end = timeit.default_timer() print("Merge sort took %s sec" % str(end-start)) self.assertEqual(Y, sorted(self.random_seq))
from sorts import merge_sort, shell_sort from time import time x = [1000 - i for i in range(1000)] start = time() a = print(merge_sort(x)) stop = time() print("Merge Sort time: {0}".format(stop - start)) start = time() a = shell_sort(x) stop = time() print("Shell Sort time: {0}".format(stop - start))
def merge_sort(input_file_name: str, output_file_name: str = None): numbers = get_numbers_from_file(input_file_name) sorted_numbers = sorts.merge_sort(numbers) print_numbers_to_file(sorted_numbers, output_file_name)
#!/usr/bin/env python from sorts import insertion_sort, merge_sort, heap_sort, build_heap arr = ['a', 'c', 'b', 'f', 'd', 'aa', 'zzzzzz'] print arr merge_sort(arr) print arr arr = ['a', 'c', 'b', 'f', 'd', 'aa', 'zzzzzz'] print arr insertion_sort(arr) print arr arr = ['a', 'c', 'b', 'f', 'd', 'aa', 'zzzzzz'] print arr heap_sort(arr) print arr arr = ['a', 'c', 'b', 'f', 'd', 'aa', 'zzzzzz'] print arr build_heap(arr) print arr
generator(size) # создаем файл с данными with open(f'./data_{size}.txt', mode='r', encoding='UTF-8') as database: for one_data in database: data.append(Product( one_data.strip())) # переносим данные из файла в массив key = data[random.randint(0, len(data) - 1)].name start = time() simple_find(data, key) sleep(0.001) end = time() simple_find_time = str(end - start - 0.001) start = time() merge_sort(data) binary_find(data, key) sleep(0.001) end = time() binary_find_time = str(end - start - 0.001) start = time() binary_find(data, key) sleep(0.001) end = time() binary_find_and_sort_time = str(end - start - 0.001) data_multimap = MultimapProduct(data) start = time() data_multimap.find(key) sleep(0.001)
def test_merge_sort(self): for test_line in self.test_data: merge_sorted = merge_sort(list(test_line)) tim_sorted = sorted(test_line) self.assertEqual(merge_sorted, tim_sorted, '\n\nFailed to be sorted! The input was:\n{}\n\nmerge_sort returned:\n{}\n\nand it should be:\n{}\n'.format(teal + test_line + NC, red + ''.join(merge_sorted) + NC, green + ''.join(tim_sorted) + NC))
def test_merge_sort(self): size = 170 values = sorts.get_values(size) actual = sorts.merge_sort(values, 0, size - 1) expected = sorted(values) self.assertEqual(expected, actual)
def main(argv): data = files.read_lines_of_ints(argv[0]) print ' '.join(str(item) for item in sorts.merge_sort(data[1])[0])
with open(f'./data_{size}.txt', mode='r', encoding='UTF-8') as database: for one_data in database: data.append(Product( one_data.strip())) # переносим данные из файла в массив data_bubble = copy.deepcopy(data) data_cocktail = copy.deepcopy(data) data_merge = copy.deepcopy(data) start1 = datetime.now() bubble_sort(data_bubble) # сортировка пузырьком end1 = datetime.now() start2 = datetime.now() cocktail_sort(data_cocktail) # шейкер-сортировка end2 = datetime.now() start3 = datetime.now() merge_sort(data_merge) # сортировка слиянием end3 = datetime.now() print(str(size) + 'bubble sort:' + str(end1 - start1) + '\n') print(str(size) + 'cocktail sort:' + str(end2 - start2) + '\n') print(str(size) + 'merge sort:' + str(end3 - start3) + '\n') with open('./time.txt', mode='a', encoding='UTF-8') as result: result.write(f"size: {size} bubble sort: {str(end1 - start1)}" + '\n') result.write(f"size: {size} cocktail sort: {str(end2 - start2)}" + '\n') result.write(f"size: {size} merge sort: {str(end3 - start3)}" + '\n')
def main(argv): n, A = files.read_lines_of_ints(argv[0]) print sorts.merge_sort(A)[1]