ABASYN UNIVERSITY PESHAWAR
DEPARTMENT OF COMPUTER SCIENCE
Assignment ( 4 )
Subject :- Data Structure
Class /section:-BScs/CA
Submitted to:-Sir Tufail Muhammad
Submitted by:- Syed Imad Shah 16036
Sequential search Algorithm:-
Steps:- 1)
loc = -1
2) Input data
3) Repeat step 4 for i=1 to N by 1
4) if item=abc[i] then.
Loc=i
Print(“Data is found”,loc)
5) if loc =-1
Print(“Data is not found”)
Sequential search implementation:-
Binary search Algorithm:-
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)
5) if item>abc[mid] then. Repeat
for i=mid+1 to N by 1
If item=abc[i] then.
Loc=i
Print(“data found”)
Else:
Repeat for i=mid-1 to 1 by -1
If item=abc[i] then.
Loc=1
Print(“data found”)
If lic=-1
Print(“data is not found”)
Binary search implementation:-
which algorithm is best and why?
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.
Q2: Write Down the Function for Binary Search?
# Binary Serach Function: def
BinarySearch(list1,location):
low=0 high=len(list1)-1
found=False while low<=high
and not found:
mid=(low+high)//2
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:
print("Location is not found")
Binary Search implementation:-