Advertisement
makispaiktis

Linear and Binary Search Execution Time

Apr 24th, 2020 (edited)
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. '''
  2. CHAPTER 1
  3. '''
  4.  
  5. def binarySearch(A, u, low, high):
  6.     if low > high:
  7.         return None
  8.     mid = (low + high) // 2
  9.     if u == A[mid]:
  10.         return mid
  11.     if u > A[mid]:
  12.         return binarySearch(A, u, mid + 1, high)
  13.     else:
  14.         return binarySearch(A, u, low, mid - 1)
  15.  
  16. def linearSearch(A, u):
  17.     n = len(A)
  18.     flag = False
  19.     for i in range(n):
  20.         if u == A[i]:
  21.             flag = True
  22.             return i
  23.     if flag == False:
  24.         return None
  25.  
  26.  
  27. # MAIN FUNCTION
  28. myList = list(range(1, 100))
  29. print("Index of 38 (binarySearch): " + str(binarySearch(myList, 38, 0, len(myList))))
  30. print("Index of 50 (binarySearch): " + str(binarySearch(myList, 50, 0, len(myList))))
  31. print("Index of 88 (binarySearch): " + str(binarySearch(myList, 88, 0, len(myList))))
  32. print("Index of -100 (binarySearch): " + str(binarySearch(myList, -100, 0, len(myList))))
  33. print("Index of 38 (linearSearch): " + str(linearSearch(myList, 38)))
  34. print("Index of 50 (linearSearch): " + str(linearSearch(myList, 50)))
  35. print("Index of 88 (linearSearch): " + str(linearSearch(myList, 88)))
  36. print("Index of -100 (linearSearch): " + str(linearSearch(myList, -100)))
  37.  
  38.  
  39. '''
  40. CHAPTER 2
  41. '''
  42.  
  43. # Create a sorted list with elements form 1 to 1.000.000
  44. newList = list(range(1, 1000001))
  45. from timeit import default_timer as timer
  46. executions = list(range(100000, 1100000, 100000))
  47. # executions = [100, 200, 300, .... , 900, 1000]
  48. measurements_lsearch = []
  49. measurements_bsearch = []
  50. for num in executions:
  51.     start1 = timer()
  52.     linearSearch(newList[0:num], -100);
  53.     end1 = timer()
  54.     measurements_lsearch.append(end1 - start1)
  55.     start2 = timer()
  56.     binarySearch(newList[0:num], -100, 0, len(newList[0:num]) - 1);
  57.     end2 = timer()
  58.     measurements_bsearch.append(end2 - start2)
  59.  
  60. print("Executions    Time_linearSearch in ms       Time_binarySearch in ms")
  61. for execution, m1, m2 in zip(executions, measurements_lsearch, measurements_bsearch):
  62.     print(str(execution) + "          " + str(m1 * 1000) + "          " + str(m2 * 1000))
  63.  
  64.  
  65.  
  66. '''
  67. CHAPTER 3
  68. '''
  69.  
  70. import matplotlib.pyplot as plt
  71. plt.plot(executions, measurements_lsearch, label = "Linear Search")
  72. plt.plot(executions, measurements_bsearch, label = "Binary Search")
  73. plt.xlabel("Number of Elements")
  74. plt.ylabel("Execution Time")
  75. plt.legend()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement