Binary Search Program
2025-11-04
binary search
searching algorithm
sorted array search
divide and conquer
logical programs
coding interview questions
python binary search
c binary search
javascript search
php binary search
csharp binary search
Binary Search is an efficient algorithm to find an element in a sorted array by repeatedly dividing the search range. This guide includes explanation, algorithm steps, and code examples in Python, C, JavaScript, C#, and PHP.
Program Explanation
What is Binary Search?
Binary Search is an efficient searching algorithm used to find an element in a sorted array by repeatedly dividing the search interval into half. It works only on sorted data and has a time complexity of O(log n).
Steps to Solve
- Take a sorted array and the target element as input.
- Set two pointers: low = 0 and high = n - 1.
- Find the mid index using (low + high) // 2.
- If the mid element equals the target → element found.
- If the target is smaller → search in the left half.
- If the target is larger → search in the right half.
- Repeat until low exceeds high.
Key Points
- Binary search works ONLY on a sorted list.
- Much faster than linear search for large datasets.
- Time Complexity: O(log n).
- Frequently asked in coding interviews.
Program Code
arr = [2, 5, 8, 12, 16, 23, 38]
target = int(input("Enter number to search: "))
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
print("Element found at index:", mid)
break
elif target < arr[mid]:
high = mid - 1
else:
low = mid + 1
else:
print("Element not found")
#include <stdio.h>
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38};
int target, low = 0, high = 6, mid;
printf("Enter number to search: ");
scanf("%d", &target);
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == target) {
printf("Element found at index: %d", mid);
return 0;
}
else if (target < arr[mid]) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
printf("Element not found");
return 0;
}
let arr = [2, 5, 8, 12, 16, 23, 38];
let target = Number(prompt("Enter number to search:"));
let low = 0, high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
console.log("Element found at index:", mid);
break;
} else if (target < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
if (low > high) console.log("Element not found");
using System;
class Program {
static void Main() {
int[] arr = {2, 5, 8, 12, 16, 23, 38};
Console.Write("Enter number to search: ");
int target = int.Parse(Console.ReadLine());
int low = 0, high = arr.Length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
Console.WriteLine("Element found at index: " + mid);
return;
}
else if (target < arr[mid]) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
Console.WriteLine("Element not found");
}
}
<?php
$arr = [2, 5, 8, 12, 16, 23, 38];
$target = intval(readline("Enter number to search: "));
$low = 0;
$high = count($arr) - 1;
while ($low <= $high) {
$mid = intdiv($low + $high, 2);
if ($arr[$mid] == $target) {
echo "Element found at index: $mid";
exit;
} elseif ($target < $arr[$mid]) {
$high = $mid - 1;
} else {
$low = $mid + 1;
}
}
echo "Element not found";
?>
