Featured Article

Binary Search Algorithm Explained with Step-by-Step Example

William Aug 14, 2026

The binary search algorithm, a cornerstone of efficient searching techniques, uses a conceptually simple yet powerful premise: divide and conquer. It's unlawleravely applicable in scenarios where data is arranged in ascending or descending order, offering rapid, near-instant results. Let's delve into the binary search algorithm, exploring its workings and practical usage with concrete examples.

What is binary search algorithm?  -GeeksforGeeks
What is binary search algorithm? -GeeksforGeeks

Before we dive into the algorithm's intricacies, it's crucial to understand the binary search's foundation. It leverages the fact that data is sorted, and the middle element is either our target or a pivot point guiding us towards the target. This targeted approach effectively slicing the search space in half with each iteration, amounting to logarithmic time complexity.

Must Know Algorithms for Coding Interviews
Must Know Algorithms for Coding Interviews

The Binary Search Process

The binary search algorithm follows a systematic approach, iteratively narrowing down the search space. It's a refinement of the linear search algorithm, yielding performance enhancements by harnessing data sorting. Let's explore its key steps:

Binary Search Explained Visually | DSA Guide for Beginners
Binary Search Explained Visually | DSA Guide for Beginners

1. Initiate the search by examining the middle element of the sorted array. 2. If the target value matches the middle element, return its index. 3. If the target is less than the middle element, move to the left half of the array. 4. If the target is greater, focus on the right half. 5. Repeat steps 1-4 until the target is found or the subarray becomes exhausted.

Understanding Key Concepts

Complete Computer Science & Cybersecurity Infographics Bundle
Complete Computer Science & Cybersecurity Infographics Bundle

Adequately grasping the binary search algorithm necessitates familiarity with essential terms and computations. Here are crucial elements:

Midpoint: The center index (index//2) of the current search interval. The algorithm recurrently uses this index for comparison and decision-making.

Low and High Bounds: Denote the starting (low) and ending (high) indices of the current search interval. As the algorithm progresses, these bounds contract around the target value.

Algorithm types and algorithm examples
Algorithm types and algorithm examples

Pseudocode for Binary Search

Here's a simplified pseudocode representation of the binary search algorithm:

function binarySearch(arr, low, high, x): while low <= high mid = low + (high - low) / 2 if arr[mid] == x return mid elseif arr[mid] < x low = mid + 1 else high = mid - 1 endif endwhile return -1

Top 20 Algorithms Interview Problems for Programmers and Software Engineers
Top 20 Algorithms Interview Problems for Programmers and Software Engineers

In the pseudocode above, 'arr' stands for the array, 'x' signifies the target value, while 'low' and 'high' represent the initial search interval's bounds. The function recursively halves the search space until the target is found or eliminated.

Binary Search Algorithm Implementation Examples

Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners
30 Days of Java – Day 25 | Linear vs Binary Search Explained for Beginners
How to Implement Binary Search Using Iterative Method
How to Implement Binary Search Using Iterative Method
Searching Algorithms Explained 🔍 | Linear vs Binary vs Jump vs Interpolation | DSA Made Easy 🚀
Searching Algorithms Explained 🔍 | Linear vs Binary vs Jump vs Interpolation | DSA Made Easy 🚀
a notebook with diagrams on how to use the search engine for an interactive learning tool
a notebook with diagrams on how to use the search engine for an interactive learning tool
Searching Algorithms in C++ – Linear vs Binary Search
Searching Algorithms in C++ – Linear vs Binary Search
Complete Computer Science & Cybersecurity Infographics Bundle
Complete Computer Science & Cybersecurity Infographics Bundle
Binary Search in Data Structure
Binary Search in Data Structure
How to code Binary Search Algorithm using Recursion in Java? Example
How to code Binary Search Algorithm using Recursion in Java? Example
💜 Linear Search vs Binary Search 🔍
💜 Linear Search vs Binary Search 🔍
Binary Search Explained Step-by-Step
Binary Search Explained Step-by-Step
Binary Search Flow Explained 🔍 | Step-by-Step DSA Guide ⚡ | Easy Example
Binary Search Flow Explained 🔍 | Step-by-Step DSA Guide ⚡ | Easy Example
Iterative and Recursive Binary Search Algorithm Implementation in Java
Iterative and Recursive Binary Search Algorithm Implementation in Java
Binary Search Algorithm Example Walkthrough
Binary Search Algorithm Example Walkthrough
Sorting & Searching Cheat Sheet 🔍 | Coding Interview Prep 💻
Sorting & Searching Cheat Sheet 🔍 | Coding Interview Prep 💻
binary search
binary search
A * search algorithm in artificial intelligence
A * search algorithm in artificial intelligence
Abdul Wahab Junaid is Offensive Security Researcher | Quantum Cryptography Analyst
Abdul Wahab Junaid is Offensive Security Researcher | Quantum Cryptography Analyst
Simple Binary search code example in cpp  ~ C++ Programming Tutorial for Beginners
Simple Binary search code example in cpp ~ C++ Programming Tutorial for Beginners

To illustrate the binary search algorithm's operation, let's explore its implementation in different programming languages.

Binary Search in Python

Python's elegant syntax and powerful built-in functions make it well-suited for binary search implementation. Here's a straightforward Python function for binary search:

def binary_search(arr, low, high, x): while low <= high: mid = low + (high - low) // 2 if arr[mid] == x: return mid elif arr[mid] < x: low = mid + 1 else: high = mid - 1 return -1

To use this function, simply call it with the array, target value, and initial low (0) and high (length of array - 1) values:

result = binary_search([2, 3, 4, 10, 40], 0, 4, 10)

Binary Search in JavaScript

In JavaScript, the binary search algorithm can be encapsulated within a standalone function. Below is a JavaScript implementation:

function binarySearch(arr, low, high, x) { while (low <= high) { let mid = low + ((high - low) >> 1); if (arr[mid] === x) return mid; arr[mid] < x ? low = mid + 1 : high = mid - 1; } return -1; }

Call the function with the array, target value, and initial search bounds:

let result = binarySearch([2, 3, 4, 10, 40], 0, 4, 10);

Binary Search in C++

C++ developers often leverage built-in Standard Template Library (STL) functions for binary search. The 'lower_bound' and 'upper_bound' templates from the 'algorithm' library return, respectively, the first element greater than and not less than the target. Here's a C++ function implementing this:

#include <algorithm>

int binary_search(vector<int> arr, int x) { return lower_bound(arr.begin(), arr.end(), x) - arr.begin(); }

This function assumes the array is sorted in ascending order. Invoke it with the array and target value:

int result = binary_search({2, 3, 4, 10, 40}, 10);

With each iteration, the binary search algorithm narrows the search space, significantly improving performance over linear search approaches. It's a must-learn for programmers and system designers, offering a concrete understanding of divide-and-conquer strategies. As binary search undergirds more complex data structures and algorithms, mastering it paves the way for advanced learning and practical problem-solving. So, keep refining your skills, and embrace the binary search algorithm's power to enhance your toolbox.