Binary Search (Iterative)
Binary Search Algorithm in C++ (Iterative Implementation)
C++ Binary Search (Iterative) Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int key) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid; // Found at index mid
}
if (arr[mid] < key) {
left = mid + 1; // Search right half
} else {
right = mid - 1; // Search left half
}
}
return -1; // Not found
}
int main() {
int arr[] = {11, 12, 22, 25, 34, 64, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Enter element to search: ";
cin >> key;
int result = binarySearch(arr, n, key);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in array" << endl;
}
return 0;
}Sorted array: 11 12 22 25 34 64 90 Enter element to search: 25 Element found at index: 3
Understanding Binary Search (Iterative)
This program teaches you how to implement the Binary Search algorithm in C++ using an iterative approach. Binary Search is a highly efficient divide-and-conquer algorithm that searches a sorted array by repeatedly dividing the search interval in half. It's much faster than Linear Search for large datasets, but requires the array to be sorted first.
---
1. What This Program Does
The program searches for an element in a sorted array using Binary Search (iterative version). For example:
Binary Search eliminates half of the remaining elements in each step, making it extremely efficient.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Binary Search
Algorithm Concept
:
Visual Example
:
Example (searching for 34):
---
4. Function: binarySearch()
int binarySearch(int arr[], int n, int key) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
}
if (arr[mid] < key) {
left = mid + 1; // Search right half
} else {
right = mid - 1; // Search left half
}
}
return -1; // Not found
}
return mid; // Found at index midHow it works
:
---
5. Step-by-Step Algorithm
Step 1: Initialize Interval
Step 2: Calculate Middle
Step 3: Compare
Step 4: Repeat
---
6. Understanding Mid Calculation
Why left + (right - left) / 2?
Example
:
---
7. Time and Space Complexity
Time Complexity
: O(log n)
Space Complexity
: O(1)
---
8. When to Use Binary Search
Best For
:
Not Recommended For
:
---
9. Important Considerations
Array Must Be Sorted
:
Loop Condition
:
Index Updates
:
---
10. Advantages Over Linear Search
Performance
:
Efficiency
:
---
11. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning efficient search algorithms, understanding divide-and-conquer techniques, and preparing for advanced searching methods in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Binary Search (Iterative)
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.