write a program that analyzes the time it takes to sort a list of numbers using
ID: 3639689 • Letter: W
Question
write a program that analyzes the time it takes to sort a list of numbers using bubble sort, insertion sort, selection sort and the default sorting algorithm in pythonExplanation / Answer
from time import* from random import* #GNOME SORT def gnome_sort(lst): pos = 1 while pos = lst[pos-1]: pos = pos+1 else: temp = lst[pos] lst[pos] = lst[pos-1] lst[pos-1] = temp if pos > 1: pos = pos - 1 else: pos = pos + 1 return lst #SELECTION SORT def smallest(lst): """Finds the smallest element in the list.""" small = lst[0] for i in range(len(lst)): if lst[i] lst[i+diff]: temp = lst[i] lst[i] = lst[i+diff] lst[i+diff] = temp else: lst[i] = lst[i] diff = 3 for i in range(len(lst)-3): if lst[i] > lst[i+diff]: temp = lst[i] lst[i] = lst[i+diff] lst[i+diff] = temp else: lst[i] = lst[i] diff = 2 for i in range(len(lst)-2): if lst[i] > lst[i+diff]: temp = lst[i] lst[i] = lst[i+diff] lst[i+diff] = temp else: lst[i] = lst[i] diff = 1 for i in range(len(lst)-1): if lst[i] > lst[i+diff]: temp = lst[i] lst[i] = lst[i+diff] lst[i+diff] = temp else: lst[i] = lst[i] return lst #QUICK SORT def quick_sort(lst): """Quick sort chooses a pivot element, places the elements greater than and less than the pivot into places relative to the pivot, and are then recursively sorted.""" lst1 = [x for x in lst[1:] if x lst[0]] if len(lst) == 0: return lst elif len(lst) == 1: return lst else: return quick_sort(lst1) + [lst[0]] + quick_sort(lst2) #SIMPLE MERGE SORT def split(lst): """This splits a list into sublists of pairs of elements.""" split_list = [] x = 0 if len(lst) % 2 == 0: while xRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.