# Code for Binary Search Algorithm (C Sharp)

What does Binary Search Algorithm do? Given a sorted array of n elements, write a function to search for the index of a given element (target).

## Process of working of Binary Search Algorithm?

• Search for the array by dividing the array in half repeatedly.
• Initially consider the actual array and pick the element at the middle index
• Keep a lower index i.e. 0 and higher index i.e. length of array
• If it is equal to the target element then return the index
• Else if it is greater than the target element then consider only the left half of array. (lower index = 0, higher = middle – 1)
• Else if it is less than the target element then consider only the right half of array. (lower index = middle + 1, higher = length of array)
• Return -(insertion index + 1) if the target element is not found in the array (If the lower index is greater than or equal to higher index). Some simpler implementations just return -1 if the element is not found. The offset of 1 must be added as the insertion index might be 0 (the searched value might be smaller than all elements in the array). As indexing starts at 0, this must be distinguishable from the case where the target element has the index 0.

## Time Complexity of Binary Search Algorithm

• O(log n) Worst Case
• O(1) Best Case (If middle element of initial array is the target element)

## Space Complexity of Binary Search Algorithm

• O(1) For iterative approach
• O(1) For recursive approach if tail call optimization is used, O(log n) due to recursion call stack, otherwise

## C Sharp (C #) Code for Binary Search Algorithm

For copying this code to your local Code Editor please click little arrow on top right corner just next to “Binary Search Algorithm” and then hit Command + C if your on Mac or Control + C if your on Windows. This will copy the code to your laptop’s Clipboard and then you can go to your local Code Editor and paste it.

``````/**
* @file
* @brief Program to perform [binary
* search](https://en.wikipedia.org/wiki/Binary_search_algorithm) of a target
* value in a given *sorted* array.
* @authors [James McDermott](https://github.com/theycallmemac) - recursive
* algorithm
* @authors [Krishna Vedala](https://github.com/kvedala) - iterative algorithm
*/
#include
#include

/** Recursive implementation
* \param[in] arr array to search
* \param l left index of search range
* \param r right index of search range
* \param x target value to search for
* \returns location of x assuming array arr[l..r] is present
* \returns -1 otherwise
*/
int binarysearch1(const int *arr, int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;

// If element is present at middle
if (arr[mid] == x)
return mid;

// If element is smaller than middle
if (arr[mid] > x)
return binarysearch1(arr, l, mid - 1, x);

// Else element is in right subarray
return binarysearch1(arr, mid + 1, r, x);
}

// When element is not present in array
return -1;
}

/** Iterative implementation
* \param[in] arr array to search
* \param l left index of search range
* \param r right index of search range
* \param x target value to search for
* \returns location of x assuming array arr[l..r] is present
* \returns -1 otherwise
*/
int binarysearch2(const int *arr, int l, int r, int x)
{
int mid = l + (r - l) / 2;

while (arr[mid] != x)
{
if (r <= l || r < 0)
return -1;

if (arr[mid] > x)
// If element is smaller than middle
r = mid - 1;
else
// Else element is in right subarray
l = mid + 1;

mid = l + (r - l) / 2;
}

// When element is not present in array
return mid;
}

/** Test implementations */
void test()
{
// give function an array to work with
int arr[] = {2, 3, 4, 10, 40};
// get size of array
int n = sizeof(arr) / sizeof(arr);

printf("Test 1.... ");
// set value to look for
int x = 10;
// set result to what is returned from binarysearch
int result = binarysearch1(arr, 0, n - 1, x);
assert(result == 3);
printf("passed recursive... ");
result = binarysearch2(arr, 0, n - 1, x);
assert(result == 3);
printf("passed iterative...\n");

printf("Test 2.... ");
x = 5;
// set result to what is returned from binarysearch
result = binarysearch1(arr, 0, n - 1, x);
assert(result == -1);
printf("passed recursive... ");
result = binarysearch2(arr, 0, n - 1, x);
assert(result == -1);
printf("passed iterative...\n");
}

/** Main function */
int main(void)
{
test();
return 0;
}``````