Question: Could you help me fix this code in python? I am not really sure what is the problem. Thanks import random import matplotlib.pyplot as plt
Could you help me fix this code in python? I am not really sure what is the problem. Thanks
import random import matplotlib.pyplot as plt import time from array import array import numpy from datetime import datetime
#Here we are printing the date-time and our name now = datetime.now() print("Your name") print("now =", now)
#Now we have created this function so that after sorting we can check whether the array is sorted correctly or not def list_sorted(l): flag = 0 for i in range(1,len(l)): #If we find any left elemnt that is greater than it's right element that means that array is not sorted and we assign flag 1 if(l[i]
if(flag==1): return False #It says the data storage isn't sorted so we return false
else: return True #It says that data storage is sorted so we return true
#Now we have created this function so that we can sort the array 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]
return lst
#Set N to this sizes N=[128, 256, 512, 1024, 2048, 4096,8192,16384] #Set variables to 0 ,these variable will be used to calculate time difference for list,python array and numpy array a1=0 b1=0 a2=0 b2=0 a3=0 b3=0 running=[]
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 #In order 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 a1=time.time() print("now working on list") #Call bubble sort function bubbleSort(num) #Store time after sorting in b b1=time.time() #Check the status ,whether it is sorted or not ans=list_sorted(num) print("python list sorted:" +str(ans)) #Similary, we are doing for python array and numpy array print("now working on arrays") python_array = array("i", num) a2=time.time() bubbleSort(python_array)
b2=time.time() ans=list_sorted(python_array) print("python array sorted:"+str(ans)) print("now working on numpy array") numpy_array=numpy.array(num) a3=time.time()
bubbleSort(numpy_array)
b3=time.time() ans=list_sorted(numpy_array) print("numpy array sorted:"+ str(ans)) #Here we are storing the differences of timing running.append([b1-a1,b2-a2,b3-a3]) #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) #We show how the plot plt.show() print(running)
The output is the following:

ValueError Traceback (most recent call last)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
