Mid From PDF Coding C# Coding Interview

Binary Search?

Follow on:

int BinarySearch(int[] arr, int target)
{
int low = 0, high = arr.Length - 1;

while (low <= high)

{
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;
}
return -1;
}

Explanation:

Standard binary search to find target’s index or -1 if not found.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details