Binary Search Iterative Approach in DSA C - Time & Space Complexity
We want to understand how the time taken by binary search changes as the size of the list grows.
How many steps does it take to find a number in a sorted list?
Analyze the time complexity of the following code snippet.
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
This code searches for a target number in a sorted array by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: The loop runs until the search range is empty, roughly cutting the range in half each step.
Each step cuts the search area in half, so the number of steps grows slowly as the list gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one extra step.
Time Complexity: O(log n)
This means the steps needed grow slowly, increasing by one each time the input size doubles.
[X] Wrong: "Binary search checks every element one by one, so it is O(n)."
[OK] Correct: Binary search does not check every element; it cuts the search area in half each time, so it needs far fewer steps.
Understanding binary search time complexity shows you can analyze efficient algorithms, a key skill in many coding challenges and real projects.
"What if the array was not sorted? How would the time complexity of searching change?"
Practice
while loop in the iterative binary search algorithm?Solution
Step 1: Understand the role of the loop
Thewhileloop runs as long as the search range is valid, narrowing down the range by half each time.Step 2: Identify the loop's goal
The loop stops when the target is found or when the search range becomes empty, meaning the target is not present.Final Answer:
To repeatedly divide the search range until the target is found or range is empty -> Option DQuick Check:
Loop narrows search range until target found or range empty [OK]
- Thinking the loop sorts the array
- Assuming the loop swaps elements
- Believing the loop prints elements
Solution
Step 1: Understand overflow risk
Addinglow + highdirectly can overflow if both are large integers.Step 2: Use safe middle calculation
Calculatinglow + (high - low) / 2avoids overflow by subtracting first.Final Answer:
int mid = low + (high - low) / 2; -> Option CQuick Check:
Safe mid calculation avoids overflow [OK]
- Using (low + high) / 2 causing overflow
- Subtracting incorrectly in middle calculation
- Multiplying indices which is wrong
int arr[] = {2, 4, 6, 8, 10};
int n = 5;
int target = 6;
int low = 0, high = n - 1;
int mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (arr[mid] == target) {
printf("%d", mid);
break;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}Solution
Step 1: Trace the binary search steps
Initial low=0, high=4, mid=2. arr[2]=6 matches target 6.Step 2: Print the mid index
Since arr[mid] == target, the code prints 2 and breaks the loop.Final Answer:
2 -> Option BQuick Check:
Target found at index 2 [OK]
- Confusing index with value
- Not updating low/high correctly
- Assuming target not found
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}Solution
Step 1: Analyze loop condition
The loop useswhile (low < high), which misses checking when low equals high.Step 2: Correct loop condition
It should bewhile (low <= high)to include the last element in search.Final Answer:
The loop condition should be while (low <= high) -> Option AQuick Check:
Loop must include low == high to check last element [OK]
- Using < instead of <= in loop condition
- Ignoring overflow in mid calculation (not here)
- Confusing return value with index
- Assuming array needs sorting inside function
int arr[] = {1, 3, 5, 7, 9, 11, 13}, write the iterative binary search steps to find the target 10. What will be the final output of the search function?Solution
Step 1: Trace search for target 10
Initial low=0, high=6, mid=3 (arr[3]=7). Since 7 < 10, low=mid+1=4.Step 2: Continue search narrowing range
Now low=4, high=6, mid=5 (arr[5]=11). Since 11 > 10, high=mid-1=4.Step 3: Final check
Now low=4, high=4, mid=4 (arr[4]=9). Since 9 < 10, low=mid+1=5. Now low=5 > high=4, loop ends.Final Answer:
-1 (target not found) -> Option AQuick Check:
Target 10 not in array, returns -1 [OK]
- Assuming closest smaller element index is returned
- Not updating low/high correctly
- Stopping search too early
Ready to prove it?
Pass the test and this pattern moves unknown
