Featured Article

C++ Binary Search: A Step-by-Step Example

William Aug 14, 2026

Binary search is a fundamental algorithm in computer science, widely used for searching a sorted array. It significantly reduces the time complexity of searching compared to a linear search, making it an essential tool for sorting and finding elements in data structures. Let's explore binary search with a practical C++ example.

💜 Linear Search vs Binary Search 🔍
💜 Linear Search vs Binary Search 🔍

Binary search uses the divide-and-conquer strategy to narrow down the search space by half in each iteration. The algorithm compares the target value with the middle element of the searched array. If they are equal, it returns the index. If the target is less than the middle element, it discards the right half of the array; if it's greater, it eliminates the left half.

Linear And Binary Search Array in C - Probabu
Linear And Binary Search Array in C - Probabu

The C++ Binary Search Algorithm

The C++ implementation of the binary search algorithm often follows these steps:

Searching Algorithms in C++ – Linear vs Binary Search
Searching Algorithms in C++ – Linear vs Binary Search

1. Define lower and upper bounds (usually denoted as 'start' and 'end').

2. Enter a loop that continues as long as the lower bound is less than or equal to the upper bound.

binary search
binary search

Finding the Middle Element

To find the middle element, the C++ implementation typically uses the formula 'start + (end - start) / 2'.

In some cases, to avoid integer overflow, the formula can be simplified to 'start + (end - start) / 2'. The '(end - start) / 2' part is added and subtracted at the same time, so it's equivalent to multiplying by '(end - start) / 2' instead of dividing by 2.

Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews
Binary Search Explained 🔍 | DSA Algorithm Guide for Coding Interviews

Comparing and Adjusting Bounds

The algorithm compares the target value with the middle element. If they are equal, it returns the index. If the target is less than the middle element, it adjusts the upper bound to one less than the middle; if the target is greater, it modifies the lower bound to one more than the middle.

This process continues until the target value is found or the search space is eliminated.

binary search algorithm
binary search algorithm

Implementing Binary Search in C++

Here's a simple C++ binary search function that demonstrates the algorithm:

Binary Search Explained Visually | DSA Guide for Beginners
Binary Search Explained Visually | DSA Guide for Beginners
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 🚀
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
How to Implement Binary Search Using Iterative Method
How to Implement Binary Search Using Iterative Method
Binary
Binary
Understanding The Binary Search Algorithm In Python | Towards Data Science
Understanding The Binary Search Algorithm In Python | Towards Data Science
a spiral notebook with an image of a tree diagram on the front and bottom page
a spiral notebook with an image of a tree diagram on the front and bottom page
Binary Search in Data Structure
Binary Search in Data Structure
Three Things to Know about Binary Search | Towards Data Science
Three Things to Know about Binary Search | Towards Data Science
Binary search lookups - Excel Dashboard School
Binary search lookups - Excel Dashboard School
Binary Search in Data Structure
Binary Search in Data Structure
a table with numbers and an arrow pointing to it
a table with numbers and an arrow pointing to it
Iterative and Recursive Binary Search Algorithm Implementation in Java
Iterative and Recursive Binary Search Algorithm Implementation in Java
Binary Search Trees and Recursion
Binary Search Trees and Recursion
Binary Search in Java for Sorted Array
Binary Search in Java for Sorted Array
How to Implement Binary Search Using Iterative Method
How to Implement Binary Search Using Iterative Method
Introduction to Binary Search Trees with Python
Introduction to Binary Search Trees with Python

int binary_search(int arr[], int size, int target) {

int start = 0, end = size - 1;

while (start <= end) {

int mid = start + (end - start) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

start = mid + 1;

} else {

end = mid - 1;

}

}

return -1; // Element not found

}

Testing the Binary Search Function

To use this function, pass an sorted integer array, its size, and the target value. The function will return the index of the target value if found, or -1 if not.

int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};

int size = sizeof(arr) / sizeof(arr[0]);

int target = 23;

int index = binary_search(arr, size, target);

if (index != -1) {

std::cout << "Element found at index: " << index << std::endl;

} else {

std::cout << "Element not found." << std::endl;

}

Explore and understand the binary search algorithm's intricacies. It's an essential tool for всяких начинающих и профессиональных разработчиков.