import random import time import sys import math def merge(first_sublist, second_sublist): i = j = 0 merged_list = [] while i < len(first_sublist) and j < len(second_sublist): if first_sublist[i] < second_sublist[j]: merged_list.append(first_sublist[i]) i += 1 else: merged_list.append(second_sublist[j]) j += 1 while i < len(first_sublist): merged_list.append(first_sublist[i]) i += 1 while j < len(second_sublist): merged_list.append(second_sublist[j]) j += 1 return merged_list def merge_sort(unsorted_list): if len(unsorted_list) == 1: return unsorted_list mid_point = int(len(unsorted_list)/2) first_half = unsorted_list[:mid_point] second_half = unsorted_list[mid_point:] half_a = merge_sort(first_half) half_b = merge_sort(second_half) return merge(half_a, half_b) def insertion_sort(l): for index in range(1, len(l)): search_index = index insert_value = l[index] while search_index > 0 and l[search_index-1] > insert_value: l[search_index] = l[search_index-1] search_index -= 1 l[search_index] = insert_value random.seed(42) loriginal = [random.randint(0, 2**18) for i in range(3_000_000)] lordenado = loriginal.copy() lordenado.sort() l = loriginal.copy() starttime = time.time() l = merge_sort(l) print("Elapsed Time in Merge Sort: " + str(time.time() - starttime)) print(l == lordenado)