Advertisement
makispaiktis

Count execution time of my sorting method

Apr 24th, 2020 (edited)
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. '''
  2. CHAPTER 1
  3. '''
  4. def selectionSort(A):
  5.     n = len(A)
  6.     for j in range(0, n-1):
  7.         for i in range(j+1, n):
  8.             if A[j] > A[i]:
  9.                 A[j], A[i] = A[i], A[j]
  10.  
  11.     return A
  12.  
  13. # MAIN FUNCTION - 1
  14. myList = [2, 4, 5, 1, 6, 3]
  15. print("Initial list: " + str(myList))
  16. print("Sorted list with my method: " + str(selectionSort(myList)))
  17. print("Sorted list with Python's default method: " + str(sorted(myList)))
  18. print()
  19.  
  20. '''
  21. CHAPTER 2
  22. '''
  23. import random
  24. # Create a list of 3000 integers in range 1 .... 1000
  25. newList = [random.randint(1, 1000) for i in range(3000)]
  26. from timeit import default_timer as timer
  27. executions = list(range(100, 3100, 100))
  28. measurements = []
  29. # executions = [100, 200, 300, ...., 2900, 3000]
  30. print()
  31. print("~~~~ This process will take less than 10 seconds. Just wait ~~~~")
  32. print()
  33. print()
  34.  
  35. for num in executions:
  36.     start = timer()
  37.     selectionSort(newList[0: num])
  38.     end = timer()
  39.     measurements.append(end - start)
  40.  
  41. print("Elements    Execution Time")
  42. for i in range(len(executions)):
  43.     print(str(executions[i]) + "        " + str(measurements[i] * 1000) + "ms")
  44.  
  45.  
  46. '''
  47. CHAPTER 3
  48. '''
  49.  
  50. # Plots time
  51. import matplotlib.pyplot as plt
  52. plt.plot(executions, measurements)
  53. plt.xlabel("Number of Elements")
  54. plt.ylabel("Execution Time")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement