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\le i$$ and for each $$i+k\le j\le n$$, $$a_i\le 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\le \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$$.

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\le i < n-k$$, to keep track of $$\max(a_j\mid j\le i)$$ and to check that $$a_{i+k}\ge\max(a_j\mid j\le 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\ge i) < \max(a_k\mid k\le 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^1_n]$$ and $$\alpha=[a^2_1,\dots, a^2_m]$$ 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^1_i\mid 1\le i\le k/2)$$ and $$A^2=\max(a^2_i\mid 1\le i\le 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^1_{k/2}$$ from $$\alpha^1$$ and remove $$\{a^2_j\mid 1\le j\le k/2, a^2_j\le 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.