ArticleslgStudy

science

Cycle sort

Cycle sort is a 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 Cycle sort rather than just read about it. In short: Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.

Cycle sort — main illustration
Cycle sort — illustration

Key takeaways

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

Reference excerpt

Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result. Unlike nearly every other sort, items are never written elsewhere in the array simply to push them out of the way of the action. Each value is either written zero times, if it is already in its correct position, or written one time to its correct position. This matches the minimal number of overwrites required for a completed in-place sort. Minimizing the number of writes is useful when making writes to some huge data set is very expensive, such as with EEPROMs like Flash memory where each write reduces the lifespan of the memory.

Algorithm To illustrate the idea of cycle sort, consider a list with distinct elements. Given an element x, we can find the index at which it will occur in the sorted list by simply counting the number of elements in the entire list that are smaller than x. Now

If the element is already at the correct position, do nothing. If it is not, we will write it to its intended position. That position is inhabited by a different element y, which we then have to move to its correct position. This process of displacing elements to their correct positions continues until an element is moved to the original position of x. This completes a cycle.

Repeating this process for every element sorts the list, with a single writing operation if and only if an element is not already at its correct position. While computing the correct positions takes O(n) time for every single element, thus resulting in a quadratic time algorithm, the number of writing operations is minimized.

Implementation To create a working implementation from the above outline, two issues need to be addressed:

When computing the correct positions, we have to make sure not to double-count the first element of the cycle. If there are duplicate elements present, when we try to move an element x to its correct position, that position might already be inhabited by an x. Simply swapping these would cause the algorithm to cycle indefinitely. Instead, we have to insert the element after any of its duplicates. The following Python implementation performs cycle sort on an array, counting the number of writes to that array that were needed to sort it. Python

The next implementation written in C++ simply performs cyclic array sorting.

Situation-specific optimizations When the array contains only duplicates of a relatively small number of items, a constant-time perfect hash function can greatly speed up finding where to put an item1, turning the sort from Θ(n2) time to Θ(n + k) time, where k is the total number of hashes. The array ends up sorted in the order of the hashes, so choosing a hash function that gives you the right ordering is important. Before the sort, create a histogram, sorted by hash, counting the number of occurrences of each hash in the array. Then create a table with the cumulative sum of each entry in the histogram. The cumulative sum table will then contain the position in the array of each element. The proper place of elements can then be found by a constant-time hashing and cumulative sum table lookup rather than a linear search.

References

External links ^ "Cycle-Sort: A Linear Sorting Method", The Computer Journal (1990) 33 (4): 365-367.[link removed]

Original source of unrestricted variant Cyclesort - a curious little sorting algorithm

Illustrations

Cycle sort: Example of cycle sort sorting a list of random numbers.
Example of cycle sort sorting a list of random numbers.
Cycle sort: Displacement cycle for list "bdeac", when shifting the first letter b to its correct position:
Displacement cycle for list "bdeac", when shifting the first letter b to its correct position:

Worked examples

Example 1 — a first encounter with Cycle sort

Start with the simplest possible case. Write down what Cycle sort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In 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 Cycle sort 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 Cycle sort 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 Cycle sort

In research
Cycle sort appears in 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 Cycle sort 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
Cycle sort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Comparison sorts, so understanding it makes those chapters shorter.
In everyday life
Look for Cycle sort 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 “Cycle sort” →

How to study Cycle sort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cycle sort 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 Cycle sort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cycle sort in simple terms?

Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sor…

Why does Cycle sort matter?

Because it connects several 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 Cycle sort?

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 Cycle sort.

Tags

  • Comparison sorts

Keep exploring