0% found this document useful (0 votes)
16 views8 pages

SE Sem IV AoA Lab Experiment 5 202425

The document outlines Experiment No. 5, which focuses on the implementation of the Binary Search Algorithm, emphasizing its efficiency in locating a target value within a sorted array using a divide and conquer approach. It details the working principle, steps involved in the algorithm, and provides examples along with a discussion of its best, worst, and average case complexities. The document concludes with a comparison of binary and linear search methods.

Uploaded by

Jagruti Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

SE Sem IV AoA Lab Experiment 5 202425

The document outlines Experiment No. 5, which focuses on the implementation of the Binary Search Algorithm, emphasizing its efficiency in locating a target value within a sorted array using a divide and conquer approach. It details the working principle, steps involved in the algorithm, and provides examples along with a discussion of its best, worst, and average case complexities. The document concludes with a comparison of binary and linear search methods.

Uploaded by

Jagruti Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No.

5
To implement Binary Search Algorithm
Date of Performance:
Date of Submission:

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer Engineering


Experiment No. 5
Title: Binary Search Algorithm

Aim: To study and implement Binary Search Algorithm Objective: To introduce


Divide and Conquer based algorithms

Theory:

Binary search is a highly efficient algorithm used to locate a target value within a
sorted array. It works by repeatedly dividing the search interval in half until the target
value is found or the search interval is empty.

Working Principle:

Binary search relies on the fact that the array is sorted. It compares the target value
with the middle element of the array. If the target value matches the middle element,
the search is successful. If the target value is less than the middle element, the search
continues on the left half of the array. If the target value is greater, the search
continues on the right half.

Steps of Binary Search:

Step 1:

Initialize two pointers, low and high, to the first and last indices of the array
respectively.

Let low = 0 and high = n - 1 (where n is the size of the array)

Step 2:

Repeat the following steps until low is less than or equal to high.

Calculate the middle index: mid = (low + high) / 2.

Compare the target value with the middle element arr[mid].

If the target value equals arr[mid],

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
return mid

If the target value is less than arr[mid],

update high = mid - 1 (search the left half).

If the target value is greater than arr[mid],

update low = mid + 1 (search the right half).

Step 3:

If the search interval becomes empty (i.e., low exceeds high), the target value is not
present in the array. Return a sentinel value (e.g., -1) to indicate that the value was
not found.

Example:

Let's say we want to search for the value 12 in array [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

using the binary search algorithm.

Initialize two pointers, low and high, to the first and last indices of the array.

low = 0, high = 9 (for an array of size 10).


Pass Find Middle Compare arr[mid] with the Update Pointers
No. Element target value
mid = Compare arr[mid] with the Since the target value is
(low+high)/2. target value (12). greater than the middle
1
mid = (0+9)/2 arr[4] = 10 is less than 12, element.

= 4. Indicating that target value in Update low to mid + 1.

the right half of array. low = mid + 1 = 4 + 1 = 5.

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
mid arr[7] = 16 is greater than 12, Update high to mid - 1.
2 =(low+high)/2. indicating that the target value high = mid -1

mid =(5+9)/2=7. lies in the left half of the =7-1


remaining array. = 6.

mid = (low + arr[5] = 12 matches the target ------------


3 high) / 2. value.

mid = (5 + 6) / 2
= 5.

Result:

Return the index of the found element (5 in this case).

Binary search algorithm successfully located the target value 12 in the array.

Algorithm and Complexity:

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
Best Case:

• In binary search, the key is initially compared to the array’s middle element.

• If the key is in the center of the array, the algorithm only does one comparison,
regardless of the size of the array.

• As a result, the algorithm’s best-case running time is T(n) = 1.

Worst Case:

• Every iteration, the binary search, search space is decreased by half, allowing for
maximum log2n array divisions.

• If the key is at the leaf of the tree or it is not present at all, then the algorithm does
log2n comparisons, which is maximum.

• The number of comparisons increases in logarithmic proportion to the amount of


the input. As a result, the algorithm’s worst-case running time would be T(n) =
O(log2 n).

• The problem size is reduced by a factor of two after each iteration, and the
method does one comparison.

• Recurrence of binary search can be written as T(n) = T(n/2) + 1. Solution to this


recurrence leads to same running time, i.e. O(log2n). Detail derivation is
discussed here:

• In every iteration, the binary search does one comparison and creates a new
problem of size n/2.

• So, recurrence equation is,

T(n) = T(n/2) + 1, if n > 1

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
T(n) = 1, if n = 1

• Only one comparison is needed when there is only one element in the array.

• Let solve by iterative approach,

T (n) = T(n/2) + 1 …(1)

put n by n/2 in Equation (1) to find T(n/2)

T(n/2) = T(n/4) + 1 …(2)

put value of T(n/2) in Equation (1),

T(n) = T(n/22) + 1 …(3)

put n by n/2 in Equation (2) to find T(n/4),

T(n/4) = T(n/8) + 1

Use value of T(n/4) in Equation (3),

T(n) = T(n/23) + 3

.....

After k iterations,

T(n) = T(n/ 2k) + k --- (4)

Assume, n/ 2k =1 so, n = 2k

Take log from both sides

Log n = log 2k => Log n = k log22

{ Using log22 = 1 }

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
Use k = log2n in Equation (4),

T(n) = T(1) + log n

= 1 + log n {Ignore constant part}

T(n) = O(log2n)

Average Case:
• The average case for binary search occurs when the key element is neither in the
middle nor at the leaf level of the search tree.
• On average, it does half of the log2 n comparisons, which will turn out as
T (n) = O(log2 n).

• The complexity of linear search and binary search for all three cases is compared
in the following table.

Search Method Best case Average case Worst case

Binary Search O(1) O(log2n) O(log2n)

Linear Search O(1) O(n) O(n)

Code:

Output:

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering
Conclusion:

Sem IV, Analysis of Algorithm Lab (CSL401) Department of Computer


Engineering

You might also like