# K-sorted sequence

In computer science, a nearly-sorted sequence, also known as roughly-sorted sequence and as $k$-sorted sequence is a sequence which is almost ordered. By almost ordered, it is meant that no element of the sequence is very far away from where it would be if the sequence were perfectly ordered. It is still possible that no element of the sequence is at the place where it should be if the sequence were perfectly ordered.
The nearly-sorted sequences are particularly useful when the exact order of element has little importance. For example Twitter nearly sort tweets, up to the second, as there is no need for more precision. Actually, given the impossibility to exactly synchronize all computers, an exact sorting of all tweets according to the time at which they are posted is impossible. This idea led to the creation of Snowflake IDs.
$k$-sorting is the operation of reordering the elements of a sequence so that it becomes $k$-sorted. $k$-sorting is generally more efficient than sorting. Similarly, sorting a sequence is easier if it is known that the sequence is $k$-sorted. So if a program needs only to consider $k$-sorted sequences as input or output, considering $k$-sorted sequences may save time.
The radius of a sequence is a measure of presortedness, that is, its value indicate how much the elements in the list has to be moved to get a totally sorted value. In the above example of tweets which are sorted up to the second, the radius is bounded by the number of tweets in a second.

## Definition

Given a positive number $k$, a sequence $[a_{1},\dots ,a_{n}]$ is said to be $k$-sorted if for each $1\leq i$ and for each $i+k\leq j\leq n$, $a_{i}\leq a_{j}$. That is, the sequence has to be ordered only for pairs of elements whose distance is at least $k$.
The radius of the sequence $\alpha$, denoted ${\text{ROUGH}}(\alpha )$ or ${\text{Par}}(\alpha )$ is the smallest $k$ such that the sequence is $k$-sorted. The radius is a measure of presortedness.
A sequence is said to be nearly-sorted or roughly-sorted if its radius is small compared to its length.

### Equivalent definition

A sequence $[a_{1},\dots ,a_{n}]$ is $k$-sorted if and only if each range of length $2k+2$, $[a_{i},a_{i+1},\dots ,a_{i+2k+2}]$ is $k$-sorted.

## Properties

All sequences of length $n$ are $(n-1)$-sorted, that is, $0\leq {\text{Par}}([a_{1},\dots ,a_{n}])<n$. A sequence is $0$-sorted if and only if it is sorted. A $k$-sorted sequence is automatically $(k+1)$-sorted but not necessarily $(k-1)$-sorted.

### Relation with sorted sequences

Given a sequence a $k$-sorted sequence $[a_{1},\dots ,a_{n}]$ and its sorted permutation $[a_{\sigma _{1}},\dots ,a_{\sigma _{n}}]$, $|i-\sigma _{i}|$ is at most $k$.

## Algorithms
### Deciding whether a sequence is $k$-sorted

Deciding whether a sequence $[a_{1},\dots ,a_{n}]$ is $k$-sorted can be done in linear time and constant space by a streaming algorithm. It suffices, for each $1\leq i<n-k$, to keep track of $\max(a_{j}\mid j\leq i)$ and to check that $a_{i+k}\geq \max(a_{j}\mid j\leq i)$.

### Computing the radius of a sequence

Computing the radius of a sequence can be computed in linear time and space. This follows from the fact that it can be defined as $\max(i-j\mid \min(a_{k}\mid k\geq i)<\max(a_{k}\mid k\leq j))$.

### Halving the radius of a sequence

Given a $2k$-sorted sequence $\alpha =[a_{1},\dots ,a_{n}]$, it is possible to compute a $(k-1)$-sorted permutation $\alpha '$ of $\alpha$ in linear time and constant space.
First, given a sequence $\beta =[b_{1},\dots ,b_{2k}]$, lets say that this sequence is partitioned if the $k$-smaller elements are in $[b_{1},\dots ,b_{k}]$ and the $k$-greater elements are in $[b_{k+1},\dots ,b_{2k}]$. Lets call partitioning the action of reordering the sequence $\beta$ into a partitioned permutation. This can be done in linear time by first finding the median of $b$ and then moving elements to the first or second half depending on whether they are smaller or greater than the median.
The sequence $\alpha '$ can be obtained by partitioning the blocks of elements at position $[2k(i-1)+1,\dots ,2ki]$, then by partitioning the blocks of elements at position $[2k(i-1)+k+1,\dots ,2ki+k]$, and then again the elements at position $[2k(i-1)+1,\dots ,2ki]$ for each number $i$ such that those sequences are defined.
Using ${\frac {n}{2k}}$ processors, with no shared read nor write access to memory, the same algorithm can be applied in $O(k)$ time, since each partition of a sequence can occur in parallel.

### Merging two $k$-sorted sequences

Merging two $k$-sorted sequences $\alpha ^{1}=[a_{1}^{1},\dots ,a_{n}^{1}]$ and $\alpha =[a_{1}^{2},\dots ,a_{m}^{2}]$ can be done in linear time and constant space.
First, using the preceding algorithm, both sequences should be transformed into $k/2$-sorted sequences.
Let's construct iteratively an output sequence $\omega$ by removing content from both $\alpha ^{i}$ and adding it in $\omega$.
If both $\alpha ^{i}$'s are empty, then it suffices to return $\omega$. Otherwise, let us assume that $\alpha ^{1}$ is empty and not $\alpha ^{2}$, it suffices to remove the content of $\alpha ^{2}$ and append it to $\omega$. The case where $\alpha ^{2}$ is empty and not $\alpha ^{1}$ is similar by symmetry.
Let us consider the case where neither $\alpha ^{i}$ is empty. Let us call $A^{1}=\max(a_{i}^{1}\mid 1\leq i\leq k/2)$ and $A^{2}=\max(a_{i}^{2}\mid 1\leq i\leq k/2)$, they are the greatest of the $k/2$-firsts elements of each list. Let us assume that $A^{1}<A^{2}$, the other case is similar by symmetry. Remove $a_{1}^{1},\dots ,a_{k/2}^{1}$ from $\alpha ^{1}$ and remove $\{a_{j}^{2}\mid 1\leq j\leq k/2,a_{j}^{2}\leq A^{1}\}$ from $\alpha ^{2}$ and add them to $\omega$.

### Sorting a $k$-sorted sequence

A $k$-sorted sequence can be sorted by applying the halving algorithm given above $\log _{2}(k)$ times. This takes $O(n\log k)$ time on a sequential machine, or $O(k\log k)$ time using $O(n)$ processors.

### $k$-sorting a sequence

Since each sequence $\alpha =[a_{1},\dots ,a_{n}]$ is necessarily $n$-sorted, it suffices to apply the halving algorithm $\log _{2}\left({\frac {n}{k}}\right)$-times. Thus, $k$-sorting can be done in $O(n\log(n/k))$-time. This algorithm is Par-optimal, that is, there exists no sequential algorithm with a better worst-case complexity.