Run of a sequence
In computer science, a run of a sequence is a non-decreasing range of the sequence that cannot be extended. The number of runs of a sequence is the number of increasing subsequences of the sequence. This is a measure of presortedness, and in particular measures how many subsequences must be merged to sort a sequence.
01Definition
Let be a sequence of elements from a totally ordered set. A run of
is a maximal increasing sequence
. That is,
and
assuming that
and
exists. For example, if
is a natural number, the sequence
has the two runs
and
.
Let be defined as the number of positions
such that
and
. It is equivalently defined as the number of runs of
minus one. This definition ensure that
, that is, the
if, and only if, the sequence
is sorted. As another example,
and
.
02Sorting sequences with a low number of runs
The function is a measure of presortedness. The natural merge sort is
-optimal. That is, if it is known that a sequence has a low number of runs, it can be efficiently sorted using the natural merge sort.
03Long runs
A long run is defined similarly to a run, except that the sequence can be either non-decreasing or non-increasing. The number of long runs is not a measure of presortedness. A sequence with a small number of long runs can be sorted efficiently by first reversing the decreasing runs and then using a natural merge sort.
Sources and credits
This article is adapted from the Wikipedia article “Run of a sequence”, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.
Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.