0% found this document useful (0 votes)
4 views5 pages

Binary Search

The Binary Search Algorithm is an efficient searching method for sorted arrays, reducing time complexity to O(log N) by repeatedly dividing the search interval in half. It can be implemented iteratively or recursively, and is faster than linear search, making it suitable for large datasets. However, it requires the data structure to be sorted and elements to be comparable.

Uploaded by

suganya.cse
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)
4 views5 pages

Binary Search

The Binary Search Algorithm is an efficient searching method for sorted arrays, reducing time complexity to O(log N) by repeatedly dividing the search interval in half. It can be implemented iteratively or recursively, and is faster than linear search, making it suitable for large datasets. However, it requires the data structure to be sorted and elements to be comparable.

Uploaded by

suganya.cse
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/ 5

Binary Search Algorithm is a searching algorithm used in a

sorted array by repeatedly dividing the search interval in


half. The idea of binary search is to use the information that the
array is sorted and reduce the time complexity to O(log N).

What is Binary Search Algorithm?


Binary search is a search algorithm used to find the position of
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 interval is empty. The search interval is halved by
comparing the target element with the middle value of the
search space.
Conditions to apply Binary Search
Algorithm in a Data Structure:
To apply Binary Search algorithm:
 The data structure must be sorted.
 Access to any element of the data structure should take
constant time.
Binary Search Algorithm:
Below is the step-by-step algorithm for Binary Search:
 Divide the search space into two halves by finding the
middle index “mid”.
 Compare the middle element of the search space with
the key.
 If the key is found at middle element, the process is
terminated.
 If the key is not found at middle element, choose which
half will be used as the next search space.
o If the key is smaller than the middle
element, then the left side is used for next
search.
o If the key is larger than the middle
element, then the right side is used for
next search.
 This process is continued until the key is found or the
total search space is exhausted.
How does Binary Search Algorithm work?
To understand the working of binary search, consider the
following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72,
91}, and the target = 23.
2/4
How to Implement Binary Search
Algorithm?
The Binary Search Algorithm can be implemented in the
following two ways
 Iterative Binary Search Algorithm
 Recursive Binary Search Algorithm
Given below are the pseudocodes for the approaches.
Iterative Binary Search Algorithm:
Here we use a while loop to continue the process of comparing
the key and splitting the search space in two halves.
Implementation of Iterative Binary Search Algorithm:
// C program to implement iterative Binary Search
#include <stdio.h>

// An iterative binary search function.


int binarySearch(int arr[], int low, int high, int x)
{
while (low <= high) {
int mid = low + (high - low) / 2;

// Check if x is present at mid


if (arr[mid] == x)
return mid;

// If x greater, ignore left half


if (arr[mid] < x)
low = mid + 1;
// If x is smaller, ignore right half
else
high = mid - 1;
}

// If we reach here, then element was not present


return -1;
}

// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}

Output
Element is present at index 3

Time Complexity

Case Time Complexity

Best Case O(1)

Average Case O(logn)

Worst Case O(logn)

Recursive Binary Search Algorithm:


Create a recursive function and compare the mid of the search
space with the key. And based on the result either return the
index where the key is found or call the recursive function for
the next search space.
Implementation of Recursive Binary Search Algorithm:
// C program to implement recursive Binary Search
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[low..high] is present,
// otherwise -1
int binarySearch(int arr[], int low, int high, int x)
{
if (high >= low) {
int mid = low + (high - low) / 2;

// If the element is present at the middle


// itself
if (arr[mid] == x)
return mid;

// If element is smaller than mid, then


// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);

// Else the element can only be present


// in right subarray
return binarySearch(arr, mid + 1, high, x);
}

// We reach here when element is not


// present in array
return -1;
}

// Driver code
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

Output
Element is present at index 3

Complexity Analysis of Binary Search Algorithm:


 Time Complexity:
o Best Case: O(1)
o Average Case: O(log N)
o Worst Case: O(log N)
 Auxiliary Space: O(1), If the recursive call stack is
considered then the auxiliary space will be O(logN).
Applications of Binary Search Algorithm:
 Binary search can be used as a building block for more
complex algorithms used in machine learning, such as
algorithms for training neural networks or finding the
optimal hyperparameters for a model.
 It can be used for searching in computer graphics such
as algorithms for ray tracing or texture mapping.
 It can be used for searching a database.
Advantages of Binary Search:
 Binary search is faster than linear search, especially for
large arrays.
 More efficient than other searching algorithms with a
similar time complexity, such as interpolation search or
exponential search.
 Binary search is well-suited for searching large datasets
that are stored in external memory, such as on a hard
drive or in the cloud.
Disadvantages of Binary Search:
 The array should be sorted.
 Binary search requires that the data structure being
searched be stored in contiguous memory locations.
 Binary search requires that the elements of the array be
comparable, meaning that they must be able to be
ordered.

You might also like