Below is an explanation of binary search’s time complexity along with example implementations in several popular programming languages.
Time Complexity Overview
Binary search works on a sorted array by repeatedly dividing the search interval in half. At each iteration, it compares the target value with the middle element of the interval; based on the comparison, it discards the half in which the target cannot lie. Because the search interval is halved at each step, the worst-case time complexity of binary search is:
[
O(\log n)
]
where ( n ) is the number of elements in the array.
Example Implementations
Python
def binary_search(arr, target):
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = (lo + hi) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
# Example usage:
arr = [1, 2, 3, 4, 5, 6, 7, 8]
target = 5
index = binary_search(arr, target)
print("Found at index:", index)
Time Complexity: ( O(\log n) )
C
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int lo = 0, hi = size - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
lo = mid + 1;
else
hi = mid - 1;
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 5;
int index = binarySearch(arr, size, target);
if (index != -1)
printf("Found at index %d\n", index);
else
printf("Not found\n");
return 0;
}
Time Complexity: ( O(\log n) )
C++
#include <iostream>
#include <vector>
using namespace std;
int binarySearch(const vector<int>& arr, int target) {
int lo = 0, hi = arr.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
lo = mid + 1;
else
hi = mid - 1;
}
return -1;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8};
int target = 5;
int index = binarySearch(arr, target);
if (index != -1)
cout << "Found at index " << index << endl;
else
cout << "Not found" << endl;
return 0;
}
Time Complexity: ( O(\log n) )
Java
public class BinarySearchExample {
public static int binarySearch(int[] arr, int target) {
int lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
lo = mid + 1;
else
hi = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int target = 5;
int index = binarySearch(arr, target);
if (index != -1)
System.out.println("Found at index " + index);
else
System.out.println("Not found");
}
}
Time Complexity: ( O(\log n) )
JavaScript
function binarySearch(arr, target) {
let lo = 0, hi = arr.length - 1;
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}
// Example usage:
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
const target = 5;
const index = binarySearch(arr, target);
console.log("Found at index:", index);
Time Complexity: ( O(\log n) )
Summary
No matter which programming language you choose, the binary search algorithm consistently runs in ( O(\log n) ) time in the worst case. This logarithmic time complexity is one of the key advantages of binary search compared to a linear search, which has ( O(n) ) time complexity.
Feel free to use any of the above code examples as a reference for implementing binary search in your language of choice!