ArticleslgStudy

computer science

Rao–Sandelius shuffle

Rao–Sandelius shuffle 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 Rao–Sandelius shuffle rather than just read about it. In short: The Rao–Sandelius shuffle is a divide-and-conquer algorithm for shuffling a finite sequence. The algorithm takes a list of all the elements of the sequence, and for each element draws a uniform random value from 0 to k-1.

Key takeaways

  • Rao–Sandelius shuffle 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 Rao–Sandelius shuffle to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Rao–Sandelius shuffle from memory before moving on to harder problems.

Reference excerpt

The Rao–Sandelius shuffle is a divide-and-conquer algorithm for shuffling a finite sequence. The algorithm takes a list of all the elements of the sequence, and for each element draws a uniform random value from 0 to k-1. The list is broken up into k subsequences by the random value chosen, which are further shuffled. For an array with n {\displaystyle n} entries, the algorithm consumes O ( n log ⁡ n ) {\displaystyle O(n\log n)} random digits and performs O ( n log ⁡ n ) {\displaystyle O(n\log n)} swaps. This appears worse than the Fisher-Yates Shuffle, which consumes

O ( n log ⁡ n ) {\displaystyle O(n\log n)} random digits and performs O ( n ) {\displaystyle O(n)} swaps. However, because of the random access pattern of the Fisher-Yates shuffle, the Rao-Sandelius Shuffle can be faster for large n {\displaystyle n} due to its cache friendliness.

Original method using random digit table Rao described an algorithm for shuffling the numbers 1 through n using a table of random decimal digits. Sandelius described a procedure for shuffling a deck with n cards using random decimal digits. Sandelius includes the optimization that for a subdeck of length 2, you only need a single random digit. In the original, the order is kept for an odd random digit, and swapped for an even random digit. In either case the algorithm is as follows:

For each element to be shuffled, select a random digit 0 through 9. Group elements that selected the same random digit into sub-lists. Place the sub-lists in numerical order based on the digit selected. Repeat on any sub-lists of size 2 or greater.

Length 2 optimization Sandelius included the following optimization: If the sub-list is of length 2, draw a single random digit. Swap the order if the digit is even, and leave it unchanged if the digit is odd.

Implementation with random bits It is straightforward to adapt a version of this algorithm to a computer using k=2.

For each element to be shuffled, select a random bit. Place all the elements that selected 0 ahead of all elements that selected 1. Repeat on the 2 sub-lists. This step can be parallelized. The Size-2 speedup can also be applied in this case. Preserving the order if 1 is chosen, and swapping if 0 is chosen. Here is pseudocode for an in-place version of the algorithm (for a zero-based array):

function rs_shuffle(A, n): if n < 2 then return if n = 2 then: b ← uniform random bit if b = 0 then exchange A[0] and A[1] return rs_shuffle_large(A, n)

function rs_shuffle_large(A, n): front ← 0 // Invariant 1: front is the index of the first // non-shuffled element back ← n - 1 // Invariant 2: back is the index of the last // non-shuffled element outer: while true: b ← uniform random bit while b ≠ 1 front ← front + 1 if front > back then break outer // (*) front is now the index of an element // that belongs at the back. b ← uniform random bit while b ≠ 0 back ← back - 1 // Different due to (*) above if front ≥ back then break outer exchange A[front] and A[back] // Restore Invariant 1 front ← front + 1 // Restore Invariant 2 back ← back - 1 if front > back then break outer // Because the two halves are disjoint, these // two calls could be done in parallel rs_shuffle(A, front) rs_shuffle(A + front, n - front)

The above uses a variation on the Hoare partition scheme to reduce the average number of swaps. Each algorithm pass is effectively an Inverse-GSR 2-shuffle.

Performance For large (e.g. 10 9 {\displaystyle 10^{9}} items) data sets, the RS shuffle outperforms the more common Fisher-Yates Shuffle. It does this for two reasons:

It exhibits much better cache locality. It is parallelizable. The Fisher-Yates shuffle is not. Performance vs Fisher-Yates was measured by the authors of the MergeShuffle algorithm and by the authors of the VarPhilox GPU Shuffle

References

Worked examples

Example 1 — a first encounter with Rao–Sandelius shuffle

Start with the simplest possible case. Write down what Rao–Sandelius shuffle 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 Rao–Sandelius shuffle 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 Rao–Sandelius shuffle 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 Rao–Sandelius shuffle

In research
Rao–Sandelius shuffle 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 Rao–Sandelius shuffle 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
Rao–Sandelius shuffle is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2024 in computing, Combinatorial algorithms, Divide-and-conquer algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Rao–Sandelius shuffle 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 “Rao–Sandelius shuffle” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Rao–Sandelius shuffle in 20 minutes

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

Frequently asked questions

What is Rao–Sandelius shuffle in simple terms?

The Rao–Sandelius shuffle is a divide-and-conquer algorithm for shuffling a finite sequence. The algorithm takes a list of all the elements of the sequence, and for each element draws a uniform random value from 0 to k-1.

Why does Rao–Sandelius shuffle 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 Rao–Sandelius shuffle?

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 Rao–Sandelius shuffle.

Tags

  • 2024 in computing
  • Combinatorial algorithms
  • Divide-and-conquer algorithms
  • Randomized algorithms

Keep exploring