Binary Search in thealgorithms: Duplicate Value Index Selection Uncertainty
0 reputation · 21 Aug 2020, 20:36 UTC
Binary Search in thealgorithms: Duplicate Value Index Selection Uncertainty
The binary_search function in thealgorithms is documented to return the index of a matching element in a sorted list. However, when the target value appears multiple times, the algorithm’s recursive implementation does not specify whether it will return the first, last, or any of the duplicate indices. This ambiguity can lead to inconsistent behavior in client code that relies on a deterministic duplicate handling strategy.
Additionally, the recursive approach imposes a maximum recursion depth that may be exceeded on very large input lists, potentially raising RecursionError. The library offers no configuration to switch to an iterative variant or to adjust recursion limits.
Given these uncertainties, the following questions remain unresolved:
- What deterministic index—first, last, or arbitrary—should the algorithm return when duplicates exist, and how can callers enforce a specific choice?
- Is there an intended iterative implementation or a recommended workaround to avoid recursion limits on large datasets?