0% found this document useful (0 votes)
6 views9 pages

AOA Experiment 5

The document outlines Experiment No. 5, focusing on the implementation of the Binary Search Algorithm, which efficiently locates a target value in a sorted array using a divide and conquer approach. It details the algorithm's working principle, steps, and complexity analysis, highlighting its best, worst, and average case scenarios. The provided code demonstrates the binary search implementation, concluding that the algorithm is efficient with a time complexity of O(log n) and a space complexity of O(1).

Uploaded by

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

AOA Experiment 5

The document outlines Experiment No. 5, focusing on the implementation of the Binary Search Algorithm, which efficiently locates a target value in a sorted array using a divide and conquer approach. It details the algorithm's working principle, steps, and complexity analysis, highlighting its best, worst, and average case scenarios. The provided code demonstrates the binary search implementation, concluding that the algorithm is efficient with a time complexity of O(log n) and a space complexity of O(1).

Uploaded by

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

Experiment No.

5
To implement Binary Search Algorithm
Date of Performance: 12/02/2025
Date of Submission: 05/03/2025

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.

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.

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


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

Best Case:

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

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


Engineering
• 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

T(n) = 1, if n = 1

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

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


Engineering
• 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 }

Use k = log2n in Equation (4),

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

= 1 + log n {Ignore constant part}

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


Engineering
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)

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


Engineering
Code:
#include <stdio.h>

void binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1, mid, step = 1;

while (low <= high) {


mid = (low + high) / 2;
printf("Iteration %d: low = %d, mid = %d, high = %d, mid_value = %d\n",
step++, low, mid, high, arr[mid]);
if (arr[mid] == key) {
printf("Element %d found at index %d\n", key, mid);
return;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("Element %d not found in the array\n", key);
}

int main() {
int n, key;

printf("Enter the size of the array: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");

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


Engineering
scanf("%d", &key);
printf("\n--- Iteration Steps ---\n");
binarySearch(arr, n, key);

return 0;
}

Output:

Conclusion:
Binary Search is an efficient algorithm for searching an element in a sorted array by
repeatedly dividing the search space in half. With a time complexity of O(log n), it
significantly outperforms linear search for large datasets. The algorithm iteratively adjusts the
search range by comparing the middle element, reducing the number of comparisons needed.
Its O(1) space complexity makes it memory-efficient, as it does not require additional storage.

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


Engineering

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy