You are on page 1of 8

Lab Report 02

Rajshahi University of Engineering & Technology

Department of Computer Science and Engineering

CSE 2202

Sessional Based on 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

Submitted to: Submitted by:


Name: Rizoan Toufiq, Name: Rukaiya Haque
Assistant Professor, Roll: 1903120
Department of CSE, Section: B
RUET 2nd Year Odd Semester

1|P age
Lab Report 02

INDEX

Sl.No Topic Page No.

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:

Processor Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz 1.19 GHz

Installed RAM 12.0 GB (11.8 GB usable)

System type 64-bit operating system, x64-based processor

Title: Complexity analysis of searching algorithm (Linear search and Binary search)

Objective:

• Complexity analysis of Linear Search Algorithm.


• Complexity analysis of Binary Search Algorithm.

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.

Repeat this loop:

If i > n, then exit the loop.

If A[i] = x, then exit the loop.

Set i to i + 1.

Return i.

3|P age
Lab Report 02

Recursive

LinearSearch(value, list)

if the list is empty, return Λ;

else

if the first item of the list has the desired value, return its location;

else return LinearSearch(value, remainder of the list)

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.

• Otherwise, narrow it to the upper half.

• Repeatedly check from the second point until the value is found or the interval is empty.

Binary Search Algorithm can be implemented in the following two ways

1. Iterative Method

2. Recursive Method

1. Iteration Method

binarySearch(arr, x, low, high)

repeat till low = high

mid = (low + high)/2

if (x == arr[mid])

return mid

else if (x > arr[mid]) // x is on the right side

4|P age
Lab Report 02

low = mid + 1

else // x is on the left side

high = mid - 1

2. Recursive Method (The recursive method follows the divide and conquer approach)

binarySearch(arr, x, low, high)

if low > high

return False

else

mid = (low + high) / 2

if x == arr[mid]

return mid

else if x > arr[mid] // x is on the right side

return binarySearch(arr, x, mid + 1, high)

else // x is on the left side

return binarySearch(arr, x, low, mid - 1)

5|P age
Lab Report 02

Data Table:

Linear Search:

X-axis Y-axis Y-axis


No of Data Time(Iterative) Time(Rcursive)
2500 0 0
5000 0 0.016
10000 0 0.2375
12500 0 0.078
15000 0 0.172
17500 0 0.36
20000 0 0.377
22500 0 0.799
25000 0 0.188

Binary Search:

No of Data. Ruquired Time(Iterative) Required Time(Recursive)

10000000 0.016 0.282

20000000 0.062 0.356

30000000 0.063 0.369

40000000 0.078 0.453

50000000 0.082 0.521

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

You might also like