Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

Draw a schematic diagram of I.C. engines and name the parts.

Answered: 1 week ago

Question

7. List behaviors to improve effective leadership in meetings

Answered: 1 week ago

Question

6. Explain the six-step group decision process

Answered: 1 week ago