DevMaking About Dark
Light
Solarized Light
Solarized

Binary Search Examples: Java Python PHP C# C++ TypeScript
▸ Binary Search Quick Review

# Binary Search in C#

``````using System;

public static class BinarySearch
{

// Assumes 'arr' is in sorted order.

// Iterative implementation of Binary Search
public static int BinarySearchIterative(int[] arr, int num)
{
int mid, low, high;
low = 0;
high = arr.Length - 1;

while(low &lt;= high)
{

mid = (low + high) / 2;

if(num &lt; arr[mid])
{
high = mid - 1;
}
else if(num &gt; arr[mid])
{
low = mid + 1;
}
else
{
return mid;
}
}
return -1;
}

// Recursive implementation of Binary Search
public static int BinarySearchRecursive(int[] arr, int num)
{
return BinarySearchRecursiveAux(arr, num, 0, arr.Length  - 1);
}
// Helper method for the recursive approach.
private static int BinarySearchRecursiveAux(int[] arr, int num, int low, int high)
{
int mid =  (low + high) / 2;

if(low &gt;= high) return -1;

if (num &lt; arr[mid])
{
return BinarySearchRecursiveAux(arr, num, low, mid - 1);
}
else if (num &gt; arr[mid])
{
return BinarySearchRecursiveAux(arr, num, mid + 1, high);
}
else
{
return mid;
}
}
}
``````

Find any bugs in the code? let us know!