A search through sorted robot records can return a perfectly valid index even when the requested key is absent. Lower-bound search finds a partition boundary: the first position whose key is not less than the query.
That operation is useful for insertion, duplicate ranges, and interval counts. Treating the returned position as an automatic match can instead select the wrong record or access beyond the end of the list.
The finite keys and traces below are original hypothetical data. They are not a deployed robot log, a timing benchmark, or results from running Python or C++ library functions.
Define the boundary before the algorithm
Use the sorted key list a=[2,4,4,4,9], with zero-based positions zero through four. A lower bound for query x is an index i between zero and five such that every earlier key is less than x and every key from i onward is greater than or equal to x.
An upper bound instead places every earlier key at most x and every later key strictly greater than x. The different equality condition determines which side of repeated keys the boundary occupies.
Python's bisect documentation states those left and right insertion partitions. The C++ working draft defines lower_bound through a partition predicate and its returned iterator. The original list makes their mathematical meaning explicit without claiming execution.
Find the two boundaries around duplicates
For x=4, the lower bound is position one. The key at position zero is two, while positions one through four contain values at least four.
The upper bound is position four, immediately before nine. Positions one, two, and three all equal four. Their count is upper minus lower, 4−1=3.
Neither boundary chooses a particular duplicate's application meaning. If three records share the key four, another rule is needed to identify a specific record among them. The boundary only identifies where the equal-key block begins or ends.
For x=5, both boundaries are four. Five belongs between the final four and nine, but no key equals five. A boundary at position four is therefore valid for both query four's upper edge and query five's insertion location while representing different questions.
Trace a half-open lower-bound search
Maintain a candidate interval [lo,hi), initialized to [0,5). At each step take mid=floor((lo+hi)/2). If a[mid]<x, replace lo with mid+1; otherwise replace hi with mid. Stop when lo=hi.
For x=4, the first midpoint is two, whose key is four. Because it is not less than four, hi becomes two. The next midpoint is one, again four, so hi becomes one.
The final midpoint is zero, whose key two is less than four. Set lo to one. Both boundaries now equal one, the desired first not-less position.
The interval is half-open, but the answer can equal its original upper endpoint. The algorithm searches possible boundary positions rather than requiring a matching element to exist at every possible answer.
State the invariant that justifies discarding positions
At every stage, positions before lo are already known to hold keys less than x. Positions at or after hi are known to hold keys at least x. Only the interval between them remains undecided.
If a[mid]<x in a sorted list, all positions up to mid also have keys less than x, so discarding them preserves the boundary. Otherwise, the first not-less position cannot lie after mid, so reducing hi to mid preserves it.
Each update shortens the candidate interval. Using lo=mid instead of mid+1 on the less-than branch could leave a one-position interval unchanged and prevent progress.
Check membership separately
For ordinary finite numeric keys, after obtaining lower-bound index i, an exact membership check needs both i<len(a) and a[i]=x. The first condition protects the endpoint, and the second establishes equality.
For x=5, i=4 is in range but a[4]=9, so membership is false. For x=12, i=5 equals the list length; there is no element at that position to compare.
For x=0, i=0 is a valid insertion boundary before all existing keys. For an empty list, the sole boundary is zero. These outcomes follow from the partition definition without inventing a special matching record.
Use boundaries to count intervals
The number of keys in the half-open value interval [4,9) is lower_bound(9)−lower_bound(4)=4−1=3. It includes the three fours and excludes nine.
The number in the inclusive value interval [4,9] is upper_bound(9)−lower_bound(4)=5−1=4. Changing the requested endpoint inclusion changes which search boundary is needed.
These are value intervals, distinct from the index interval [lo,hi) maintained inside the algorithm. Using the same bracket notation for both requires labeling what the endpoints represent.
The ordering premise is part of the result
Consider the unsorted list [4,2,9,4,4] and manually apply the same lower-bound steps for x=4. Midpoint two gives nine, reducing hi to two. Midpoint one gives two, raising lo to two. The trace ends at position two, whose value is nine, despite fours appearing elsewhere.
That is an algorithm trace outside the sorted-list premise, not an asserted library output or a valid global partition. The discarded portions did not have the order needed to justify the invariant.
The C++ draft phrases its precondition as partitioning with respect to the query comparison; complete sorting is a sufficient condition for all queries. Keep the relevant API's actual preconditions with its boundary interpretation.
Finally, logarithmic comparison count does not make insertion into an array logarithmic. Moving later entries can take linear work. Preserve the ordering, equality convention, endpoint policy, and distinction between finding a position and modifying the collection when interpreting a sorted-record search.
Sources checked October 24, 2026:
https://docs.python.org/3/library/bisect.html — Python bisect documentation displayed as Python 3.15.1 on access; left/right partition definitions, insertion-point semantics, and separate equality lookup read. No installed-version execution claimed.
https://eel.is/c++draft/lower.bound — C++ working draft section lower.bound as accessed; partition precondition, returned boundary and logarithmic comparison limit read. No fixed ISO publication version or execution is claimed.
Illustration note: A generic robot and laptop workspace is a concept illustration. It does not show these invented inputs, a measured robot result, or an executed software test.