0% found this document useful (0 votes)
26 views

Data Sructure Assignment 4

The document discusses sequential search and binary search algorithms. It provides the steps for each algorithm and an example implementation for sequential search. Binary search is more efficient than sequential search as it has a time complexity of O(log n) but requires the data to be sorted.

Uploaded by

Chota Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Data Sructure Assignment 4

The document discusses sequential search and binary search algorithms. It provides the steps for each algorithm and an example implementation for sequential search. Binary search is more efficient than sequential search as it has a time complexity of O(log n) but requires the data to be sorted.

Uploaded by

Chota Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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:-

You might also like