Question
Create a Python program that will measure the execution time of your Bubble Sort code for varying problem sizes, using Python Lists, Python Arrays, and
Create a Python program that will measure the execution time of your Bubble Sort code for varying problem sizes, using Python Lists, Python Arrays, and NumPy arrays for all problem sizes, and plot the results of all three sets of timings in one 3-variable bar chat using Matplotlib.pyplot.
Use the provided code as a starting point for this assignment.
Create 3 new Python functions that:
1. Run the problem set with Python Lists, and return the timings as a Python List.
2. Run the problem set with Python Arrays, and return the timings as a Python List.
3. Run the problem set with NumPy arrays, and return the timings as a Python List.
The argument list for each of these three functions should be identical: the same arguments are needed by all three functions to set up the List, Array, or NumPy array, fill it with random numbers, sort it while measuring elapsed timing, and returning the timing results to the caller.
Problem sizes:
For development and testing, please use N=[128, 256, 512, 1024, 2048, 4096] for your problem sizes.
For the "actual homework runs", please use N=[128, 256, 512, 1024, 2048, 4096, 8192, 16384] as your problem sizes. (The 8K run could take 6-10 seconds, and the 16K run could take 30-60 seconds.)
Creating Lists/Python Arrays/NumPy arrays to be sorted:
Use the Python random module and a hard-coded seed value to generate random numbers in the range [0..9999]. You can use your source from the provided code, modified as needed to work with Python Arrays and NumPy arrays.
What Does My Program Need to Print Out?
1. Your name and the current date+time (as in all previous assignments).
2. Inside each function for working with Lists, Python Arrays, and NumPy Arrays:
Print out a statement saying you are now working on Lists, Python Arrays, or NumPy arrays. Then, within each of these 3 functions, for each problem size:
- Print out that your code is working on problem size N[I] (the actual size of the problem)
- Print out the status of whether or not the list is sorted after you call your Bubble Sort function to sort it (in this assignment, you are no longer printing the sorted status before doing the sort).
The provided code that needs to be modified for this assignment is the following:
#importing modules import random import matplotlib.pyplot as plt import time #function to check if l is sorted or not def list_sorted(l): flag = 0 #loop over l for i in range(1,len(l)): #if this happens if(l[i] < l[i - 1]): flag = 1#set flag to 1 #if flag=1 if(flag==1): return False#return false #otherwise else: return True#return true
# bubble sort method which sort the lst in ascending order def bubbleSort(lst): s = len(lst) #bubble sort outer loop for i in range(s-1): #inner loop for j in range(0,s-i-1): #if element at index j > element at index j+1 if(lst[j] > lst[j+1]): lst[j],lst[j+1] = lst[j+1],lst[j]#swap both #return return #set N to this sizes N=[128, 256, 512, 1024, 2048, 4096] #set a and b to 0 a=0 b=0 running=[] #seed of 100 random.seed(100) #loop over N for n in N: num=[] #loop from i=0 to n for i in range(n): #using random.random() function to generate random number and then multiply it by 10000 #so as to get random number in the range 0-9999 #and convert the random float number to integer and then append it in num num.append(int(random.random()*10000)) #store time before sorting in a a=time.time() #call bubble sort function bubbleSort(num) #store time after sorting in b b=time.time() #appenf b-a in running list running.append(b-a) #use plt to plot N and running plt.plot(N,running) #set title , xlabel and ylabel plt.title("Running time of bubble sort for different input sizes") plt.xlabel("Problem size") plt.ylabel("Running time in milliseconds") #set x scale to log plt.xscale('log') #set xticks plt.xticks(N) #show the plot plt.show()
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