Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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