C# Program for Binary Search with Example and Explanation
while loop and if conditions; for example, int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }.Examples
How to Think About It
Algorithm
Code
using System; class Program { static int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } static void Main() { int[] numbers = {1, 3, 5, 7, 9}; int target = 5; int result = BinarySearch(numbers, target); Console.WriteLine(result); } }
Dry Run
Let's trace searching for target 5 in array [1, 3, 5, 7, 9].
Initialize pointers
left = 0, right = 4 (array length - 1)
Calculate mid
mid = 0 + (4 - 0) / 2 = 2
Compare mid element
arr[2] = 5 equals target 5, return 2
| left | right | mid | arr[mid] | action |
|---|---|---|---|---|
| 0 | 4 | 2 | 5 | Found target, return 2 |
Why This Works
Step 1: Divide and conquer
The code splits the array into halves by calculating the middle index with mid = left + (right - left) / 2 to avoid overflow.
Step 2: Compare middle element
It compares the middle element with the target using if statements to decide which half to search next.
Step 3: Adjust search range
If the target is greater, it moves the left pointer to mid + 1, else moves the right pointer to mid - 1, narrowing the search.
Step 4: Return result
If the target is found, it returns the index; if not found after the loop ends, it returns -1.
Alternative Approaches
using System; class Program { static int BinarySearch(int[] arr, int target, int left, int right) { if (left > right) return -1; int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) return BinarySearch(arr, target, mid + 1, right); else return BinarySearch(arr, target, left, mid - 1); } static void Main() { int[] numbers = {1, 3, 5, 7, 9}; int target = 5; int result = BinarySearch(numbers, target, 0, numbers.Length - 1); Console.WriteLine(result); } }
using System; class Program { static void Main() { int[] numbers = {1, 3, 5, 7, 9}; int target = 5; int result = Array.BinarySearch(numbers, target); Console.WriteLine(result >= 0 ? result : -1); } }
Complexity: O(log n) time, O(1) space
Time Complexity
Binary search divides the search space in half each time, so it runs in logarithmic time, O(log n), where n is the number of elements.
Space Complexity
The iterative version uses constant extra space O(1) as it only stores a few variables; recursive version uses O(log n) due to call stack.
Which Approach is Fastest?
The iterative approach is fastest and uses less memory; built-in methods are optimized but hide the logic; recursion is elegant but uses more stack.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative Binary Search | O(log n) | O(1) | Memory efficient and fast |
| Recursive Binary Search | O(log n) | O(log n) | Clear logic but uses stack |
| Array.BinarySearch Method | O(log n) | O(1) | Quick use of built-in optimized method |