What is the space complexity of binary search when implemented iteratively?
Practice Questions
Q1
What is the space complexity of binary search when implemented iteratively?
O(n)
O(log n)
O(1)
O(n log n)
Questions & Step-by-Step Solutions
What is the space complexity of binary search when implemented iteratively?
Step 1: Understand what space complexity means. Space complexity measures how much memory an algorithm uses based on the size of the input.
Step 2: Know that binary search is an algorithm used to find an item in a sorted list by repeatedly dividing the search interval in half.
Step 3: Recognize that an iterative implementation of binary search uses a few variables to keep track of the current search range (like low, high, and mid).
Step 4: Realize that these variables do not depend on the size of the input list; they always take up the same amount of space.
Step 5: Conclude that since the amount of space used does not change with the input size, the space complexity is constant.
Step 6: Therefore, the space complexity of iterative binary search is O(1).