
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 should 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#.
Working of Binary Search
Below is an animation of working of binary search in a sorted array to find the element 42:
Here are some example scenarios of searching an element using binary search:
Scenario 1
Input: arr[] = {1, 3, 5, 7, 9}, target = 7 Output: Element 7 found at index: 3
Scenario 2
Input: arr[] = {2, 4, 6, 8, 10}, target = 5 Output: 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 four steps until the left index becomes greater than the right index. If the element is found, return the index otherwise, return element not found.
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); Console.WriteLine("Element " + target + " found at index: " + result); } }
The output of the above code is as follows:
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 element not found 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 four steps until the left index becomes greater than the right index. If the element is found, return the index otherwise, return element not found.
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); Console.WriteLine("Element " + target + " found at index: " + result); } }
The output of the above code is as follows:
Given array: 2 4 6 8 10 Target Element to search: 6 Element 6 found at index: 2
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) |