mhrabbi

Binary Search Algorithm py

Jun 9th, 2020 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. # @Rabbi
  2.  
  3. def BinarySearch(ArrList,SearchItem):
  4.     UpperBound = len(ArrList)-1
  5.     LowerBound = 0
  6.     Midpoint = 0
  7.  
  8.     while LowerBound <= UpperBound:
  9.         Midpoint = (LowerBound + UpperBound) // 2
  10.         if ArrList[Midpoint] < SearchItem:
  11.             LowerBound = Midpoint + 1
  12.         elif ArrList[Midpoint] > SearchItem:
  13.             UpperBound = Midpoint - 1
  14.         if ArrList[Midpoint] == SearchItem:
  15.             return 'Found'
  16.     return 'Not Found'
  17.  
  18. RandomList = [24, 45, 64, 44, 32, 45, 11, 4, 5,1,66]
  19. SortedList = sorted(RandomList)
  20. print(BinarySearch(SortedList,322))
Add Comment
Please, Sign In to add comment