An insertion position preserves order but does not prove that a value is present

Searching an ordered sequence can answer two different questions. One asks whether a value is already present. The other asks where that value could be inserted while keeping the sequence ordered. These questions are related, but their answers are not interchangeable.

An insertion position exists even when the requested value is absent. It describes a boundary in the ordered sequence, not necessarily an observation that matches the request. Reading that boundary as a confirmed match can give an ordinary lookup the wrong meaning.

A position between two observations

Consider the ascending sequence two, five, five, five and nine. A request for seven has an insertion position after the last five and before nine. Using zero-based indexing, that boundary is position four.

Seven does not occur in the sequence. Position four still has a clear purpose: inserting seven there would preserve ascending order. The value currently stored at index four is nine, so treating the returned position as proof of equality would be incorrect.

Insertion boundaries around repeated values and the position for an absent value.

A request for five introduces another choice. Five already occupies a block of three equal values. An insertion before that block and an insertion after that block would both preserve order. The requested side decides which boundary is returned.

The left boundary is position one, before the first five. The right boundary is position four, after the last five. These are insertion positions in the original sequence, not indices of two different matching observations. The right boundary points just beyond the matching block.

Equal values make the boundary rule visible

A left insertion position separates values strictly smaller than the requested value from values greater than or equal to it. A right insertion position separates values smaller than or equal to the requested value from values strictly greater than it.

When no equal value occurs, those two positions coincide. For seven, both boundaries are four. When equal values occur, the boundaries enclose the entire block of matches. For five, the block starts at one and ends just before four.

Subtracting the left boundary from the right boundary therefore counts the matching observations in this ordered example. Four minus one gives three. The subtraction works because both boundaries refer to the same sequence, ordering rule and requested value.

Boundaries include the two ends

A sequence containing five observations has six possible insertion boundaries. A new value can go before every observation, between neighboring observations or after every observation. The last boundary is consequently numbered five with zero-based indexing.

That last number is a valid insertion position but not a valid element index for the original sequence. The existing element indices run from zero through four. Before reading an element at an insertion position, a program must confirm that the position is inside that range.

A value below two belongs at boundary zero in the example. A value above nine belongs at boundary five. These results do not indicate missing boundaries or a failed calculation. They describe the two ordinary ends of the ordered sequence.

The ordering requirement is part of the method

The boundary rule relies on the sequence being ordered according to the comparison used by the search. Applying the same procedure to an arbitrary arrangement does not make that arrangement ordered. A plausible-looking integer alone cannot confirm that the prerequisite held.

If a separate index describes the sorted order, an insertion position belongs to that sorted view. It does not automatically identify the corresponding row position in the original table. Moving from the sorted view back to original records requires the mapping that connects them.

Associated columns must keep their relationships during any sorting step. Reordering a column of values while leaving names or times unchanged can detach each value from its record. The search might then operate on ordered numbers while the table itself no longer describes the intended observations.

Give the result the name it deserves

An exact-match lookup can use a left insertion boundary as a starting point. It still needs a range check and an equality comparison with the value at that position. These are separate checks because insertion alone answers only where a value belongs.

A clear explanation states the ordering direction, indexing convention and boundary side. It also distinguishes an insertion position, an existing element index and an original record identifier. Those labels prevent a small integer from silently changing its meaning between steps.

Academia Master Trader shares educational material about data, ordering and numerical rules at academiamastertrader.com.br.

Aprenda