C Program for Binary Search with Example and Explanation
while and if statements; for example, int binarySearch(int arr[], int n, int x) { int low=0, high=n-1; while(low<=high) { int mid=low+(high-low)/2; if(arr[mid]==x) return mid; else if(arr[mid]. Examples
How to Think About It
Algorithm
Code
#include <stdio.h> int binarySearch(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return mid; else if (arr[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int x = 6; int result = binarySearch(arr, n, x); if (result != -1) printf("Element found at index %d\n", result); else printf("Element not found in the array\n"); return 0; }
Dry Run
Let's trace searching for 6 in the array {2, 4, 6, 8, 10} using binary search.
Initialize pointers
low = 0, high = 4 (array indices)
Calculate mid
mid = 0 + (4 - 0) / 2 = 2
Compare mid element
arr[2] = 6 equals target 6, return index 2
| low | high | mid | arr[mid] | comparison |
|---|---|---|---|---|
| 0 | 4 | 2 | 6 | 6 == 6, found |
Why This Works
Step 1: Divide and conquer
Binary search works by dividing the search range in half each time, reducing the problem size quickly.
Step 2: Middle element check
Checking the middle element lets us decide which half to keep searching, because the array is sorted.
Step 3: Adjust search range
If the middle element is less than the target, we search the right half; if more, the left half.
Step 4: Stop condition
When low exceeds high, the target is not in the array, so we return -1.
Alternative Approaches
#include <stdio.h> int binarySearchRecursive(int arr[], int low, int high, int x) { if (low > high) return -1; int mid = low + (high - low) / 2; if (arr[mid] == x) return mid; else if (arr[mid] < x) return binarySearchRecursive(arr, mid + 1, high, x); else return binarySearchRecursive(arr, low, mid - 1, x); } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int x = 6; int result = binarySearchRecursive(arr, 0, n - 1, x); if (result != -1) printf("Element found at index %d\n", result); else printf("Element not found in the array\n"); return 0; }
#include <stdio.h> int linearSearch(int arr[], int n, int x) { for (int i = 0; i < n; i++) { if (arr[i] == x) return i; } return -1; } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int x = 6; int result = linearSearch(arr, n, x); if (result != -1) printf("Element found at index %d\n", result); else printf("Element not found in the array\n"); return 0; }
Complexity: O(log n) time, O(1) space
Time Complexity
Binary search divides the search space in half each step, so it runs in logarithmic time, O(log n).
Space Complexity
The iterative version uses constant extra space, O(1), as it only stores a few variables.
Which Approach is Fastest?
Iterative binary search is fastest and uses less memory than recursive; linear search is slower but simpler.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative Binary Search | O(log n) | O(1) | Large sorted arrays, memory efficient |
| Recursive Binary Search | O(log n) | O(log n) | Readable code, small arrays |
| Linear Search | O(n) | O(1) | Unsorted or small arrays |