Data Sructure Assignment 4
Data Sructure Assignment 4
Assignment ( 4 )
Steps:- 1)
loc = -1
2) Input data
4) if item=abc[i] then.
Loc=i
Print(“Data is found”,loc)
5) if loc =-1
Steps:-
1) loc=-1
2) input data
3) mid=(1+N)/2
4) if item=abc[mid] then.
Loc=mid
Print(“data is found”,loc)
for i=mid+1 to N by 1
If item=abc[i] then.
Loc=i
Print(“data found”)
Else:
If item=abc[i] then.
Loc=1
Print(“data found”)
If lic=-1
Ans:-
Binary search algorithm is more efficient than linear search, it has a time complexity of
O (log n). The list of data must be in a sorted order for it to work. A binary search works
by finding the middle element of a sorted array and comparing it to your target element.
BinarySearch(list1,location):
low=0 high=len(list1)-1
if location==list1[mid]:
found=True elif
location>list1[mid]:
low=mid+1
else:
high=mid-1
if found==True:
print("Location is found")
else: