Run of a sequence

From testwiki
Jump to navigation Jump to search

Template:More footnotes

Template:More citations needed 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.

Definition

Let X=x1,,xn be a sequence of elements from a totally ordered set. A run of X is a maximal increasing sequence xi,xi+1,,xj1,xj. That is, xi1>xi and xj>xj+1Template:Clarify assuming that xi1 and xj+1 exists. For example, if n is a natural number, the sequence n+1,n+2,,2n,1,2,,n has the two runs n+1,,2n and 1,,n.

Let πš›πšžπš—πšœ(X) be defined as the number of positions i such that 1i<n and xi+1<xi. It is equivalently defined as the number of runs of X minus one. This definition ensure that πš›πšžπš—πšœ(1,2,,n)=0, that is, the πš›πšžπš—πšœ(X)=0 if, and only if, the sequence X is sorted. As another example, πš›πšžπš—πšœ(n,n1,,1)=n1 and πš›πšžπš—πšœ(2,1,4,3,,2n,2n1)=n.

Sorting 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.

Long 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.

References