You are on page 1of 7

Lab Report 03

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: 10/10/2022
Date of Submission: 17/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

INDEX
1|Page
Lab Report 03

Sl.No Topic Page No.

1. 3

Title

2. Objective 3

3. Theory 3-5

4. Table 6

5. Graph 6

6. Discussion 7

MACHINE CONFIGURATION:

2|Page
Lab Report 03

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.

Recursive

LinearSearch(value, list)
3|Page
Lab Report 03

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

low = mid + 1

else // x is on the left side

high = mid - 1

4|Page
Lab Report 03

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)

X-axis Y-axis Y-axis


No of Data Time(Iterative) Time(Rcursive)
2500 0 0
Data Table:
5000 0 0.016
Linear 10000 0 0.2375 Search:
12500 0 0.078
15000 0 0.172
17500 0 0.36
5|Page
20000 0 0.377
22500 0 0.799
25000 0 0.188
Lab Report 03

Binary Search:

Graph:

Linear Search:

6|Page
Lab Report 03

Binary Search:

Discussion:

In this lab, we have analysis the complexity of quick sort and merge sort. The main difference between
quicksort and merge sort is that the quicksort sorts the elements by comparing each element with an element
called a pivot while merge sort divides the array into two subarrays again and again until one element is left.
Sorting is the method of arranging data in a particular order.

Merge sort and quick sort are suitable for larger input sizes but quick sort perform slow .

From our graphs and experiment Quick sort algorithm is slightly faster than Merge sort algorithm and largely
faster than the Insertion sort algorithm.

7|Page

You might also like