Question
1. Download the code sort-time.py. This code measures the execution time for the sort( ) function of Python List. Tabulate and plot the execution time
1. Download the code sort-time.py. This code measures the execution time for the sort( ) function of Python List. Tabulate and plot the execution time (in ms) for various values of n (sufficient values to show the growth trend of execution time). What is the upper bound of sort( ) function for List (see section 3.6 in textbook)? Plot the upper bound versus n (in a separate plot) for the same values of n used to determine the execution time using the code. Attach the plots, brief discussion about the plots. [20]
2. In Homework 3 problem 2, you developed code to determine if there exist two numbers with sum 25, in a list of integers. Generate arbitrary size list (vary the input size, n) of integers between 1 and 25 (use the random library to generate random numbers between 1 and 25, inclusive). Measure the time taken by your code using the time library for various values of n. Tabulate the results and plot n versus execution time. Submit your code with comments. [30]
*sort time code*
import random #use the random number library import time #use time library
#generate an arbitrary list of numbers #sort the numbers, measure time
def genrand_sort(n): # n - number of rand values
rlist = [] #gen. a list of rad nums between 1 and n for i in range(n): rlist.append(random.randint(1,n))
#print(rlist) #get time before sorting init = time.time_ns()/1000000 #sort list in place rlist.sort() #get time after sorting final = time.time_ns()/1000000
print("n = ", n, "time (ms) : ", (final - init))
#print(rlist) ################################################## #call function with n = 130000 genrand_sort(130000)
*previous code*
def find(List): i = 0 while i < len(List): j = i + 1 while j < len(List): if List[i] + List[j] == 25: print (List[i] , List[j]) j += 1 i += 1 find([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,26])
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