Professional Documents
Culture Documents
Department of Computer Science and Engineering
Department of Computer Science and Engineering
CSE 2202
Lab Report 02
Name of the Experiment: Complexity analysis of searching algorithm (Linear search and
Binary search)
Date of the Experiment: 17/10/2022
Date of Submission: 24/10/2022
1|P age
Lab Report 02
INDEX
1. 3
Title
2. Objective 3
3. Theory 3-5
4. Table 6
5. Graph 7-8
6. Discussion 8
2|P age
Lab Report 02
MACHINE CONFIGURATION:
Title: Complexity analysis of searching algorithm (Linear search and Binary search)
Objective:
Theory:
Linear Search:
Linear Search is defined as a sequential search algorithm that starts at one end and goes through each
element of a list until the desired element is found, otherwise the search continues till the end of the data set.
It is the easiest searching algorithm.
Algorithm
Linear_Search(A,x)
Set i to 1.
Set i to i + 1.
Return i.
3|P age
Lab Report 02
Recursive
LinearSearch(value, list)
else
if the first item of the list has the desired value, return its location;
Binary Search:
Algorithm:
• Begin with the mid element of the whole array as a search key.
• If the value of the search key is equal to the item then return an index of the search key.
• Or if the value of the search key is less than the item in the middle of the interval, narrow the interval
to the lower half.
• Repeatedly check from the second point until the value is found or the interval is empty.
1. Iterative Method
2. Recursive Method
1. Iteration Method
if (x == arr[mid])
return mid
4|P age
Lab Report 02
low = mid + 1
high = mid - 1
2. Recursive Method (The recursive method follows the divide and conquer approach)
return False
else
if x == arr[mid]
return mid
5|P age
Lab Report 02
Data Table:
Linear Search:
Binary Search:
6|P age
Lab Report 02
Graph:
Linear Search:
7|P age
Lab Report 02
Binary Search:
Discussion:
In this lab, we have analysis the complexity of linear search and binary search. Linear search is a search that
finds an element in the list by searching the element sequentially until the element is found in the list. On the
other hand, a binary search is a search that finds the middle element in the list recursively until the middle
element is matched with a searched element.
We have analyzed linear search and binary search by using C++ and found out run time according to data
entry. From our graphs and experiment binary search is much faster than linear search.
8|P age