Back to Dashboard
Binary Search
An efficient algorithm for finding an item from a sorted list of items by repeatedly dividing the search interval in half.
Target0
Set Target
FastSlow
Initializing...
Time Complexity
Best
Average
Worst
Space Complexity
StabilityFALSE
In-placeTRUE
Explanation
What is Binary Search?
Binary Search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.
Steps
- Compare the target value to the middle element of the array.
- If the target value matches the middle element, its position has been found.
- If the target value is less than the middle element, the search continues in the lower half of the array.
- If the target value is greater than the middle element, the search continues in the upper half of the array.
Complexity
- Time Complexity:
- Space Complexity:
Pseudo Code
1left = 0, right = length - 12while left <= right3 mid = floor((left + right) / 2)4 if array[mid] == target5 return mid6 else if array[mid] < target7 left = mid + 18 else9 right = mid - 1
Sponsor Ad