daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Nov 17th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import time
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6.  
  7.  
  8. #Q3 ===========================================================================
  9. def bubblesort(mylist):
  10.     for i in reversed(range(0,len(mylist)-1)):
  11.         for j in range(1,i+1):
  12.             if mylist[j] > mylist[j+1]:
  13.                 dummy=mylist[j]
  14.                 mylist[j]=mylist[j+1]
  15.                 mylist[j+1]=dummy
  16.     return mylist
  17.  
  18. #l=list(range(1,101))
  19.  
  20. #print(bubblesort(l))
  21. #Q4 ===========================================================================
  22. def bubblesorttime(mylist):
  23.     timestart=time.perf_counter()
  24.     bubblesort(mylist)
  25.     timeend=time.perf_counter()
  26.     timetaken=timeend-timestart
  27.     return timetaken
  28.  
  29. def two_to_i_bubble(n):
  30.     for i in range(1,n+1):
  31.         mylist = list(range(2**i))
  32.         random.shuffle(mylist)  
  33.     return bubblesorttime(mylist)
  34.  
  35. x=[]
  36. y=[]
  37. for i in range(1,11):
  38.     x.append(2**i)    
  39.     y.append(two_to_i_bubble(i))
  40.    
  41. plt.figure(1)
  42. plt.loglog(x,y,marker="o")
RAW Paste Data
Top