Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3.0 code and comments please. Please run your program for a list of 1024, 4096 and 102400 integers. Part of the assignment is to

Python 3.0 code and comments please. Please run your program for a list of 1024, 4096 and 102400 integers. Part of the assignment is to generate the lists with the appropriate number, that is, don't create a sorted list to run a sort algorithm (use random).

Sorting benchmarks

Modify the modules presented (*See below) that perform the bubble sort,selesction sort, and insertion sort algorithm for 20 integers in an array, thst such that each module keeps a count of the number of swaps it makes.

Then design an application that uses three identical arrays of at least 20 integers. It should call each module on a different array, and display the number of swaps made by each algorithm.

**

Please run your program for a list of 1024, 4096 and 102400 integers. Part of the assignment is to generate the lists with the appropriate number, that is, don't create a sorted list to run a sort algorithm (use random).

Search Benchmarks

Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values.

The module should keep count of the number of comparisons it makes unt

*Module

Python program to implement linear and binary search on an array of numbers

'''

from random import randint

# function to sort the list of numbers in ascending order

def sort(list):

for i in range(0,len(list)-1):

min = i

for j in range(i+1,len(list)):

if(list[j] < list[min]):

min = j

if min != i:

temp =list[i]

list[i] = list[min]

list[min] = temp

# function to search for a number using linear search and print the number of comparisons made

def linearSearch(list,elem):

count=0

print('List : '+str(list))

for i in range(0,20):

count = count + 1

if(elem == list[i]):

break

print("No. of comparisons for linear search = "+str(count))

# function to search for a number using binary search and print the number of comparisons made

def binarySearch(list,elem):

sort(list) # sort the input array

print('List : '+str(list))

count=0

low =0

high = len(list)-1

while low<=high:

mid = int(low+high)/2

if(elem == list[mid]):

count = count + 1

break

elif(elem > list[mid]):

low = mid + 1

count = count +1

else:

high = mid -1

count = count + 1

print("No. of comparisons for binary search = "+str(count))

# test the functions

def main():

array = []

for i in range(0,20):

array.append(randint(1,20))

elem = randint(1,20)

while elem not in array :

elem = randint(1,20)

print("Element to search : "+str(elem))

linearSearch(array,elem)

binarySearch(array,elem)

main()

# end of program

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

More Books

Students also viewed these Databases questions

Question

When should you avoid using exhaust brake select all that apply

Answered: 1 week ago