Guest User

Untitled

a guest
Oct 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. def binary_search(a, s):
  2. beg = 0
  3. end = len(a) - 1
  4. while(end>=beg):
  5. mid = (beg + end) // 2
  6. if (a[mid] == s):
  7. return mid
  8. elif (a[mid] > s):
  9. end = mid - 1
  10. else:
  11. beg = mid + 1
  12. return None
  13.  
  14.  
  15. print("Enter the elements in a sorted order:",end = ' ')
  16. a = [int(i) for i in input().strip().split()]
  17. print("Enter search element: ",end = '')
  18. s = int(input())
  19.  
  20. c = binary_search(a,s)
  21.  
  22. if c == None:
  23. print(s,"is not found.")
  24. else:
  25. print(s, 'is found at position', c)
Add Comment
Please, Sign In to add comment