rishiilluri

Untitled

Aug 28th, 2022
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from datetime import datetime
  2. import matplotlib.pyplot as plt
  3.  
  4. print("EWWWW")
  5.  
  6.  
  7. f = open("input.txt", "r")
  8. input = []
  9. for k in f.readlines():
  10.     input.append(int(k[:-2]))
  11. print("Length of input ", len(input))
  12.  
  13. n = 500
  14. x_axis = []
  15. y_axis = []
  16.  
  17. while n <=10000:
  18.     total_time = 0
  19.     for i in range(3):
  20.         input2 = input[:n].copy()
  21.         t1 = datetime.now()
  22.         ########################### Buble sortuuuuu
  23.         swapped = False
  24.         for i in range(n-1):
  25.             for j in range(0, n-i-1):
  26.                 if input2[j] > input2[j + 1]:
  27.                     swapped = True
  28.                     input2[j], input2[j + 1] = input2[j + 1], input2[j]
  29.             if not swapped:
  30.                 break
  31.         ###########################
  32.  
  33.         t2 = datetime.now()
  34.         time_diff = t2-t1
  35.         time_diff = time_diff.total_seconds()
  36.         total_time += time_diff
  37.         print(time_diff, end=' ')
  38.     avg_time = total_time/3
  39.     print(avg_time)
  40.     x_axis.append(n)
  41.     y_axis.append(avg_time)
  42.     n = n + 500
  43.  
  44. plt.plot(x_axis, y_axis)
  45. plt.xticks([500*r for r in range(21)])
  46. plt.yticks([0.5*r for r in range(16)])
  47. plt.xlabel('Number of Inputs')
  48. plt.ylabel('Avg Time Taken in secs')
  49. plt.title('Bubble sort time')
  50. plt.show()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment