Searching β
Conditions of Binary Searchβ
- The data structure (array/list) must be sorted.
- The elements must allow random access (so binary search works well with arrays, but not linked lists without modifications).
Why Binary Search is Not Efficient on Linked Listβ
- In a linked list, accessing the middle element requires traversing nodes from the head β O(n).
- Binary Search depends on direct middle access (O(1) in arrays).
- Hence, overall complexity in linked lists becomes O(nlogn), worse than a simple linear search O(n).
- Special cases: binary search can still be implemented using fast and slow pointers, Use slow and fast pointers to find middle node. Then recurse/search left or right half. But itβs rarely used in practice.
Complexity of Binary Searchβ
- Time Complexity:
- Best case: O(1) (element found at first mid).
- Worst/Average: O(logn).
danger
- Space Complexity:
- Iterative: O(1).
- Recursive: O(logn) (stack calls).
Lower Boundβ
The lower bound of a target value is the first position in a sorted array where the element is greater than or equal to the target.
- If the target exists β it gives the first index of that target.
danger
- If the target doesnβt exist β it gives the index where the element could be inserted while keeping the array sorted
- Example: In
[1, 2, 4, 4, 5], lower bound of 4 = index 2. - Useful in searching with duplicates.
Upper Boundβ
The upper bound of a target value is the first position in a sorted array where the element is greater than the target.
- If the target exists multiple times β it gives the index of the element just after the last occurrence.
danger
- If the target doesnβt exist β it gives the index where an element greater than target could be inserted.
- Example: In
[1, 2, 4, 4, 5], upper bound of 4 = index 4. - Used in range queries, frequency count.
Binary Search on 2D Arraysβ
danger
- If matrix is row-wise & column-wise sorted, binary search can be applied:
- Flatten 2D into 1D index.
- Or do stepwise search (start from top-right or bottom-left).
- Example: searching element in a matrix efficiently in O(log(mΓn)).
Search in Infinite/Unknown Size Arraysβ
- When array size is not known (like an infinite stream).
- Technique:
- Start with bound = 1.
- Double bound (2, 4, 8, β¦) until arr[bound] >= target.
- Apply binary search within last known range.
- Used in problems like searching in online data streams.
Implementation Comparisonβ
- Implmentation of lower bound and upper bound is as same as Lower Bound, just modify the condition
- If
targetexists in the array:
danger
- First Occrrence = Lower Bound
- Last Occurrence = Upper Bound - 1
- Ceil = Floor =
arr[lower_bound]
0 1 2 3 4 5
-------------
1 [2 2 2] 3 4 - target = 2
^ ^
lb ub
-
First Occurrence =
lower_bound= 1 -
Last Occurrence =
upper_bound- 1 = 3 -
Ceil = Floor =
arr[lower_bound]= 2 -
If
targetdoesn't exists in the array:
danger
lower_bound=upper_bound- First Occurrence =
-1 - Last Occurrence =
-1 - Ceil =
arr[lower_bound] - Floor =
arr[lower_bound - 1]
1 2 |4 4 5 - target = 3
^
lb = ub = 2
- First Occurrence =
-1 - Last Occurrence =
-1 - Ceil =
arr[lower_bound]=4 - Floor =
arr[lower_bound - 1]=arr[1]=2