Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Binary search in C#
The binary search algorithm works on the divide and conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, the array must be sorted.
In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left sub-array, right sub-array, or return the middle element.
In this article, we are given a sorted array of integers, and our task is to search for the given target element using binary search algorithm in C#.
How Binary Search Works
Binary search repeatedly divides the search space in half by comparing the target with the middle element −
Example Scenarios
Scenario 1 − Element Found
<b>Input:</b> arr[] = {1, 3, 5, 7, 9}, target = 7
<b>Output:</b> Element 7 found at index: 3
Scenario 2 − Element Not Found
<b>Input:</b> arr[] = {2, 4, 6, 8, 10}, target = 5
<b>Output:</b> Element 5 not found
The approaches to implement binary search in C# are given below −
Binary Search Using Iteration
Below are the steps to search for an element using binary search with iterative approach −
- Define a binSearch() method that accepts the array, the size of the array, and the target that you want to search.
- Then calculate the middle index of the array.
- Compare the target with the middle element using an if-else statement. Return the middle element if the target is found at the middle index.
- If the target is greater than the middle element, then set the left index to mid + 1 and search in the right half of the array.
- If the target is less than the middle element, then set the right index to mid - 1 and search in the left half of the array.
- Repeat the above steps until the left index becomes greater than the right index. If the element is found, return the index otherwise, return -1.
Example
Here is an example code implementing the above-mentioned steps to implement binary search using iteration −
using System;
class Program {
static int binSearch(int[] arr, int n, int target) {
int left = 0, right = n - 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[] arr = {1, 3, 5, 7, 9};
int n = arr.Length;
int target = 7;
Console.WriteLine("Given array: " + string.Join(" ", arr));
Console.WriteLine("Target Element to search: " + target);
int result = binSearch(arr, n, target);
if (result != -1)
Console.WriteLine("Element " + target + " found at index: " + result);
else
Console.WriteLine("Element " + target + " not found");
}
}
The output of the above code is −
Given array: 1 3 5 7 9 Target Element to search: 7 Element 7 found at index: 3
Binary Search Using Recursion
The following are the steps to implement binary search using recursion −
- Define a binSearch() method that accepts the array, the left index, the right index, and the target that you want to search.
- Return -1 if the left index becomes greater than the right index.
- Then, calculate the middle index of the array and compare the target with the middle element using an if-else statement. Return the middle index if the target is found at the middle index.
- If the target is greater than the middle element, then recursively call the binSearch() method by setting the left index as mid + 1 and searching in the right half of the array.
- If the target is less than the middle element, then recursively call the binSearch() method by setting the right index as mid - 1 and searching in the left half of the array.
- Repeat the above steps until the element is found or the search space is exhausted.
Example
Following is an example code implementing the above-mentioned steps to search for 6 in the given array with binary search using recursion −
using System;
class Program {
static int binSearch(int[] arr, int left, int right, int target) {
if (left > right)
return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
return binSearch(arr, mid + 1, right, target);
else
return binSearch(arr, left, mid - 1, target);
}
static void Main() {
int[] arr = {2, 4, 6, 8, 10};
int n = arr.Length;
int target = 6;
Console.WriteLine("Given array: " + string.Join(" ", arr));
Console.WriteLine("Target Element to search: " + target);
int result = binSearch(arr, 0, n - 1, target);
if (result != -1)
Console.WriteLine("Element " + target + " found at index: " + result);
else
Console.WriteLine("Element " + target + " not found");
}
}
The output of the above code is −
Given array: 2 4 6 8 10 Target Element to search: 6 Element 6 found at index: 2
Using Array.BinarySearch() Method
C# provides a built-in Array.BinarySearch() method that performs binary search on a sorted array −
Example
using System;
class Program {
static void Main() {
int[] arr = {1, 3, 5, 7, 9, 11, 13};
int target = 9;
Console.WriteLine("Given array: " + string.Join(" ", arr));
Console.WriteLine("Target Element to search: " + target);
int result = Array.BinarySearch(arr, target);
if (result >= 0)
Console.WriteLine("Element " + target + " found at index: " + result);
else
Console.WriteLine("Element " + target + " not found");
}
}
The output of the above code is −
Given array: 1 3 5 7 9 11 13 Target Element to search: 9 Element 9 found at index: 4
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches −
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterative Technique | O(log n) | O(1) |
| Recursive Approach | O(log n) | O(log n) |
| Array.BinarySearch() | O(log n) | O(1) |
Conclusion
Binary search is an efficient algorithm for searching in sorted arrays with O(log n) time complexity. The iterative approach uses constant space while recursion uses O(log n) space due to call stack. C#'s built-in Array.BinarySearch() method provides a convenient alternative for binary search operations.
