Searching

 Searching

QUESTION DESCRIPTION

Given a sorted array of n elements, write a program using binary search to search a given element x in list

Input:
Number of elements, elements in sorted order and finally the element to be searched in the array.

Output:
The location where the element is found

a=int(input())
l=[]
for i in range(0,a):
  v=int(input())
  l.append(v)
s=int(input())
low=0
high=a-1
while low<=high:
  mid=(low+high)/2
  if s==l[int(mid)]:
    print(s,'found at location',int(mid)+1)
    break
  if s>l[int(mid)]:
    low=mid+1
  if s<l[int(mid)]:
    high=mid-1
if low>=high:
  print(s,'not found')
  

  

Comments