rahulkchou

Binary Search

Feb 29th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #Binary Search:
  2.  
  3. searchQuery=input("SEARCH FOR: ")
  4.  
  5. list123=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  6.  
  7.  
  8. found="false"
  9. bottomNumber=0
  10. topNumber=len(list123)-1
  11.  
  12.  
  13.  
  14.  
  15. while found=="false" and topNumber!=bottomNumber:
  16.  
  17.     if found=="true":
  18.         break
  19.     midpoint=((bottomNumber+topNumber)//2)
  20.  
  21.  
  22.    
  23.     if list123[midpoint]==searchQuery:
  24.         found="true"
  25.     elif list123[midpoint]<searchQuery:
  26.         bottomNumber=midpoint+1
  27.     else:
  28.         topNumber=midpoint+1
  29.  
  30.  
  31.  
  32.     midpoint=bottomNumber+topNumber//2
  33.  
  34.  
  35.  
  36.    
  37.     if found=="true":
  38.         print("Your search query was found in position:",midpoint+1,"!")
  39.         break
Advertisement
Add Comment
Please, Sign In to add comment