For sorting records with integer sort keys in , where , let count the input occurrences of sort key . Define two prefix sums:
The empty sum gives . Here are counts of sorting records. Exactly sorting records have smaller sort keys, so the output positions for sort key form the half-open integer interval
Its right endpoint is excluded: the last occupied array index is when .[ref]
For the seven-sorting record example, the frequency array, inclusive cumulative count array, and start array are
Thus sort keys zero, one, two and three occupy the integer intervals , , and , respectively. In particular, the three sorting records with sort key two belong at array indices three, four and five. The inclusive cumulative count counts all six sorting records with sort keys at most two; six is the exclusive endpoint, not the final position of a sorting record with sort key two.
Figure 1.
Frequencies and prefix sums reserve disjoint counting sort output ranges
. Original seven-sorting record example with . The bar heights count sorting records. The table gives the start and exclusive endpoint for each sort key; the lower strip shows zero-based output array indices. The boundary seven lies just beyond the last slot six. Details.
These integer intervals fit together because for , and . Their lengths add to , so they partition all output positions without overlaps or gaps. An absent sort key contributes an empty integer interval: with frequencies , the endpoints are and sort key two has range . For empty input, every count and endpoint is zero; every reserved integer interval is empty. This makes the same formulas cover missing sort keys and the empty case.

Articles by others on the same topic (0)

There are currently no matching articles.