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.
Let
X=\langlex1,...,xn\rangle
X
\langlexi,xi+1,...,xj-1,xj\rangle
xi-1>xi
xj>xj+1
xi-1
xj+1
n
\langlen+1,n+2,...,2n,1,2,...,n\rangle
\langlen+1,...,2n\rangle
\langle1,...,n\rangle
Let
runs(X)
i
1\lei<n
xi+1<xi
X
runs(\langle1,2,...,n\rangle)=0
runs(X)=0
X
runs(\langlen,n-1,...,1\rangle)=n-1
runs(\langle2,1,4,3,...,2n,2n-1\rangle)=n
The function
runs
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.