def run(): numbers = range(10000) datas = [] times = [] sizes = [10, 100, 500, 1000, 5000, 10000, 20000, 30000] for s in sizes: datas.append(random.choices(numbers, k=int(s))) print('Insertion Sort\n') for data in datas: startTime = time_ns() insertionSort(data) endTime = time_ns() executionTime = endTime - startTime print(len(data), 'Datas ->', executionTime, 'nanoseconds') times.append(executionTime / 1000000000) plotSort(sizes, times, 'Insertion Sort') times.clear() print('\n\nMerge Sort\n') for data in datas: startTime = time_ns() mergeSort(data, 0, len(data) - 1) endTime = time_ns() executionTime = endTime - startTime print(len(data), 'Datas ->', executionTime, 'nanoseconds') times.append(executionTime / 1000000000) plotSort(sizes, times, 'Merge Sort')
def generar_tiempos(n): lista_insertion = [random.random() for i in range(0, n)] lista_selection = copy.deepcopy(lista_insertion) lista_bubble = copy.deepcopy(lista_insertion) lista_merge = copy.deepcopy(lista_insertion) lista_quick = copy.deepcopy(lista_insertion) tiempo_1 = time.process_time() sorting.insertionSort(lista_insertion) tiempo_2 = time.process_time() sorting.selectionSort(lista_selection) tiempo_3 = time.process_time() sorting.bubbleSort(lista_bubble) tiempo_4 = time.process_time() sorting.mergeSort(lista_merge) tiempo_5 = time.process_time() sorting.quickSort(lista_quick) tiempo_6 = time.process_time() return np.asarray( (n, tiempo_2 - tiempo_1, tiempo_3 - tiempo_2, tiempo_4 - tiempo_3, tiempo_5 - tiempo_4, tiempo_6 - tiempo_5))
def main(): if len(sys.argv) > 4: problem = loadProblem(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) else: filename = utils.getOpenFileName("problem.txt") delimeter = utils.getDelimeter(",") data_type = utils.getDataType("int") problem = loadProblem(filename, delimeter, data_type) comparisson_function = utils.getComparissonFunction("lambda x,y: x>y") print("Sample items in the problem: %s" % problem[:10]) f = eval(comparisson_function) insertion_sorted = sorting.insertionSort([x for x in problem], f) merge_sorted = sorting.mergeSort([x for x in problem], f) quick_sorted = sorting.quickSort([x for x in problem], f, 0, len(problem)-1) print("insertion sorted: %s" % insertion_sorted) print("merge sorted: %s" % merge_sorted)
def main(): number_of_random_tests = 100 size_of_problems = 5 filename = "test.txt" f = lambda x,y: x>y errors = {"insertion sort":0, "merge sort":0, "quick sort":0} for _ in range(number_of_random_tests): A = random.sample(range(size_of_problems), size_of_problems) insertion_sort = sorting.insertionSort([x for x in A], f) merge_sort = sorting.mergeSort([x for x in A], f) quick_sort = [x for x in A] sorting.quickSort(quick_sort, f, 0, len(A)-1) testAlgo(A, insertion_sort, "insertion sort", errors, f) testAlgo(A, merge_sort, "merge sort", errors, f) testAlgo(A, quick_sort, "quick sort", errors, f) print("errors: \n {}".format(errors))
from sorting import insertionSort import matplotlib.pyplot as plt n = 1000 insertionTimeArrayRandom = [] insertionTimeArrayBest = [] insertionTimeArrayWorst = [] sizeArr = [] for i in range(n, n * 11, n): sizeArr.append(i) randomValues = random.sample(range(i), i) bestCase = randomValues worstCase = randomValues startTime = time() insertionSort(randomValues) endTime = time() totalTime = endTime - startTime insertionTimeArrayRandom.append(totalTime) print("For", i, "the time is", totalTime) bestCase.sort() startTime = time() insertionSort(bestCase) endTime = time() totalTime = endTime - startTime insertionTimeArrayBest.append(totalTime) print("For", i, "the time is", totalTime) worstCase.sort(reverse=True) startTime = time()
def test_insertion_sort(self): arr=[6,1,0,3,8,2,4,7,5,9] sortedArr = [0,1,2,3,4,5,6,7,8,9] insertionSort(arr) self.assertListEqual(arr,sortedArr)
import random import sorting pool = range(1,101) alist = random.sample(pool,9) print(alist) alist1 = alist sorting.insertionSort(alist1) print "insertion sort:",alist1 alist2 = alist sorting.bubbleSort(alist2) print "bubble sort:", alist2
import random import sorting pool = range(1, 101) alist = random.sample(pool, 9) print(alist) alist1 = alist sorting.insertionSort(alist1) print "insertion sort:", alist1 alist2 = alist sorting.bubbleSort(alist2) print "bubble sort:", alist2
def test_insertion_decreasing_sort(self): """ test the whole isertion sort for decreasing order """ self.assertEqual(insertionSort(self.initA, False), self.decreasing)
def test_insertion_increasing_sort(self): """ test the whole insertion sort """ self.assertEqual(insertionSort(self.initA), self.increasing)