ArticleslgStudy

computer science

K-sorted sequence

K-sorted sequence is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand K-sorted sequence rather than just read about it. In short: 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.

Key takeaways

  • K-sorted sequence belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect K-sorted sequence to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of K-sorted sequence from memory before moving on to harder problems.

Reference excerpt

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. 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 [a1,…,an] is said to be k-sorted if for each 1≤i and for each i+k≤j≤n, ai≤aj. That is, the sequence has to be ordered only for pairs of elements whose distance is at least k. The radius of the sequence α, denoted ROUGH(α) or Par(α) 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 [a1,…,an] is k-sorted if and only if each range of length 2k+2, [ai,ai+1,…,ai+2k+2] is k-sorted.

Properties All sequences of length n are (n−1)-sorted, that is, 0≤Par([a1,…,an])<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 [a1,…,an] and its sorted permutation [aσ1,…,aσn], |i−σi| is at most k.

Algorithms

Deciding whether a sequence is k-sorted Deciding whether a sequence [a1,…,an] is k-sorted can be done in linear time and constant space by a streaming algorithm. It suffices, for each 1≤i<n−k, to keep track of max⁡(aj∣j≤i) and to check that ai+k≥max⁡(aj∣j≤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∣min⁡(ak∣k≥i)<max⁡(ak∣k≤j)).

Halving the radius of a sequence Given a 2k-sorted sequence α=[a1,…,an], it is possible to compute a (k−1)-sorted permutation α′ of α in linear time and constant space. First, given a sequence β=[b1,…,b2k], lets say that this sequence is partitioned if the k-smaller elements are in [b1,…,bk] and the k-greater elements are in [bk+1,…,b2k]. Lets call partitioning the action of reordering the sequence β 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 α′ can be obtained by partitioning the blocks of elements at position [2k(i−1)+1,…,2ki], then by partitioning the blocks of elements at position [2k(i−1)+k+1,…,2ki+k], and then again the elements at position [2k(i−1)+1,…,2ki] for each number i such that those sequences are defined. Using n2k 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 α1=[a11,…,an1] and α=[a12,…,am2] 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 ω by removing content from both αi and adding it in ω. If both αi's are empty, then it suffices to return ω. Otherwise, let us assume that α1 is empty and not α2, it suffices to remove the content of α2 and append it to ω. The case where α2 is empty and not α1 is similar by symmetry. Let us consider the case where neither αi is empty. Let us call A1=max⁡(ai1∣1≤i≤k/2) and A2=max⁡(ai2∣1≤i≤k/2), they are the greatest of the k/2-firsts elements of each list. Let us assume that A1<A2, the other case is similar by symmetry. Remove a11,…,ak/21 from α1 and remove {aj2∣1≤j≤k/2,aj2≤A1} from α2 and add them to ω.

Sorting a k-sorted sequence A k-sorted sequence can be sorted by applying the halving algorithm given above log2(k) times. This takes O(nlog⁡k) time on a sequential machine, or O(klog⁡k) time using O(n) processors.

k-sorting a sequence Since each sequence α=[a1,…,an] is necessarily n-sorted, it suffices to apply the halving algorithm log2(nk)-times. Thus, k-sorting can be done in O(nlog⁡(n/k))-time. This algorithm is Par-optimal, that is, there exists no sequential algorithm with a better worst-case complexity.

Applications Nearly-sorted sequences are particularly useful when the exact order of element has little importance. For example, Twitter nearly sorts 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.

References

Worked examples

Example 1 — a first encounter with K-sorted sequence

Start with the simplest possible case. Write down what K-sorted sequence claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to K-sorted sequence before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about K-sorted sequence ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of K-sorted sequence

In research
K-sorted sequence appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses K-sorted sequence in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
K-sorted sequence is common in secondary-school and first-year university syllabi. It links to neighbouring topics Sorting algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for K-sorted sequence outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “K-sorted sequence” →

How to study K-sorted sequence in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what K-sorted sequence means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain K-sorted sequence out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is K-sorted sequence in simple terms?

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.

Why does K-sorted sequence matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study K-sorted sequence?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on K-sorted sequence.

Tags

  • Sorting algorithms

Keep exploring