Arrays
3/3 lessons
Binary Search
2/4 lessons
Sorting
1/5 lessons
Trees
0/6 lessons
Graphs
0/4 lessons
6 / 22
Total Progress
Currently Learning
Binary Search
50%
Complete
Binary Search: Time Complexity
2 min
Understanding the efficiency of binary search algorithm
What is the time complexity of binary search on a sorted array?
function binarySearch(arr, target) {
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
O(log n) - Logarithmic time
O(n) - Linear time
O(n²) - Quadratic time
O(1) - Constant time