Question
So I am supposed to create a program that compares insertion sorting and selection sorting functions. Part of the instructions are that Do not use
So I am supposed to create a program that compares insertion sorting and selection sorting functions. Part of the instructions are that "Do not use a loop to repeat the timings five times. A single run of your program should generate thirty timings: the insertion sort and selection sort timings for each of the three distributions, at each of the five lengths.
1) Plot the runtime of insertion sort and selection sort for an array of increasing values. 2. Plot the runtime of insertion sort and selection sort for an array of decreasing values. 3. Plot the runtime of insertion sort and selection sort for an array of random values.'
My code only generates it once and only in increasing order, What should I do? My code is listed 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