Question
PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting and selection sorting. My program only compares insertion and
PLEASE HELP ASAP! in Python, I am supposed to create a program that compares insertion sorting and selection sorting. My program only compares insertion and selection once. It is supposed to compare increasing, decreasing and an array of random values for each at 5 different lengths (without looping). Please help me. I have included my code below.
import time
import random
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j = j - 1
arr[j + 1] = key
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
for j in range(i + 1, len(arr)):
if arr[min_index] > arr[j]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
def main():
arr = [random.randint(1, 10000) for i in range(10000)]
dup_arr = [num for num in arr]
start = time.process_time()
insertion_sort(arr)
end = time.process_time()
print('Ten Thousand Increasing Insertion: {:.6f}'.format(end - start))
start = time.process_time()
selection_sort(dup_arr)
end = time.process_time()
print('Ten Thousand Increasing Selection: {:.6f}'.format(end - start))
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started