Question: What is wrong with my python code?!!? I cannot get it to work. import time import random def insertion_sort(arr): for i in range(1, len(arr)): key

What is wrong with my python code?!!? I cannot get it to work.

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]

'''

methods that takes individual lengths and test selection and insertion sorts.

display reports

'''

def test_random(length):

arr = [random.randint(1, length) for i in range(length)]

dup_arr = [num for num in arr]

start = time.process_time()

insertion_sort(arr)

end = time.process_time()

print(length,'Random Insertion: {:.6f}'.format(end - start))

start = time.process_time()

selection_sort(dup_arr)

end = time.process_time()

print(length,'Random Selection: {:.6f}'.format(end - start))

def test_increasing(length):

arr = [i for i in range(length)]

dup_arr = [num for num in arr]

start = time.process_time()

insertion_sort(arr)

end = time.process_time()

print(length,'Increasing Insertion: {:.6f}'.format(end - start))

start = time.process_time()

selection_sort(dup_arr)

end = time.process_time()

print(length,'Increasing Selection: {:.6f}'.format(end - start))

def test_decreasing(length):

arr = [i for i in range(length)]

dup_arr = [num for num in arr]

start = time.process_time()

insertion_sort(arr)

end = time.process_time()

print(length,'Decreasing Insertion: {:.6f}'.format(end - start))

start = time.process_time()

selection_sort(dup_arr)

end = time.process_time()

print(length,'Decreasing Selection: {:.6f}'.format(end - start))

'''

call above sort testing methods recursively

with random length.

'''

def test_rec(num_of_times):

if(num_of_times>0):

print(" Test :",num_of_times," ")

test_increasing(random.randint(1,1000))

test_decreasing(random.randint(1,1000))

test_random(random.randint(1,1000)

test_rec(num_of_times-1)

def main():

test_rec(5)

main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!