ArticleslgStudy

computer science

Samplesort

Samplesort 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 Samplesort rather than just read about it. In short: Samplesort is a sorting algorithm that is a divide and conquer algorithm often used in parallel processing systems. Conventional divide and conquer sorting algorithms partitions the array into sub-intervals or buckets.

Samplesort — main illustration
Samplesort — illustration

Key takeaways

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

Reference excerpt

Samplesort is a sorting algorithm that is a divide and conquer algorithm often used in parallel processing systems. Conventional divide and conquer sorting algorithms partitions the array into sub-intervals or buckets. The buckets are then sorted individually and then concatenated together. However, if the array is non-uniformly distributed, the performance of these sorting algorithms can be significantly throttled. Samplesort addresses this issue by selecting a sample of size s from the n-element sequence, and determining the range of the buckets by sorting the sample and choosing p−1 < s elements from the result. These elements (called splitters) then divide the array into p approximately equal-sized buckets. Samplesort is described in the 1970 paper, "Samplesort: A Sampling Approach to Minimal Storage Tree Sorting", by W. D. Frazer and A. C. McKellar.

Algorithm Samplesort is a generalization of quicksort. Where quicksort partitions its input into two parts at each step, based on a single value called the pivot, samplesort instead takes a larger sample from its input and divides its data into buckets accordingly. Like quicksort, it then recursively sorts the buckets. To devise a samplesort implementation, one needs to decide on the number of buckets p. When this is done, the actual algorithm operates in three phases:

Sample p−1 elements from the input (the splitters). Sort these; each pair of adjacent splitters then defines a bucket. Loop over the data, placing each element in the appropriate bucket. (This may mean: send it to a processor, in a multiprocessor system.) Sort each of the buckets. The full sorted output is the concatenation of the buckets. A common strategy is to set p equal to the number of processors available. The data is then distributed among the processors, which perform the sorting of buckets using some other, sequential, sorting algorithm.

Pseudocode The following listing shows the above-mentioned three step algorithm as pseudocode and shows how the algorithm works in principle. In the following, A is the unsorted data, k is the oversampling factor, discussed later, and p is the number of splitters.

function sampleSort(A[1..n], k, p) // if average bucket size is below a threshold switch to e.g. quicksort if n / k < threshold then smallSort(A) /* Step 1 */ select S = [S1, ..., S(p−1)k] randomly from // select samples sort S // sort sample [s0, s1, ..., sp−1, sp] <- [-∞, Sk, S2k, ..., S(p−1)k, ∞] // select splitters /* Step 2 */ for each a in A find j such that sj−1 < a <= sj place a in bucket bj /* Step 3 and concatenation */ return concatenate(sampleSort(b1), ..., sampleSort(bk))

The pseudo code is different from the original Frazer and McKellar algorithm. In the pseudo code, samplesort is called recursively. Frazer and McKellar called samplesort just once and used quicksort in all following iterations.

Complexity The complexity, given in Big O notation, for a parallelized implementation with p {\displaystyle p} processors: Find the splitters.

O ( n p + log ⁡ ( p ) ) {\displaystyle O\left({\frac {n}{p}}+\log(p)\right)}

Send to buckets.

O ( p ) {\displaystyle O(p)} for reading all nodes

O ( log ⁡ ( p ) ) {\displaystyle O(\log(p))} for broadcasting

O ( n p log ⁡ ( p ) ) {\displaystyle O\left({\frac {n}{p}}\log(p)\right)} for binary search for all keys

O ( n p ) {\displaystyle O\left({\frac {n}{p}}\right)} to send keys to bucket Sort buckets.

O ( c ( n p ) ) {\displaystyle O\left(c\left({\frac {n}{p}}\right)\right)} where c ( n ) {\displaystyle c(n)} is the complexity of the underlying sequential sorting method. Often c ( n ) = n log ⁡ ( n ) {\displaystyle c(n)=n\log(n)} . The number of comparisons, performed by this algorithm, approaches the information theoretical optimum log 2 ⁡ ( n ! ) {\displaystyle \log _{2}(n!)} for big input sequences. In experiments, conducted by Frazer and McKellar, the algorithm needed 15% fewer comparisons than quicksort.

Sampling the data The data may be sampled through different methods. Some methods include:

Pick evenly spaced samples. Pick randomly selected samples.

… excerpt ends here. Continue reading the full article.

Illustrations

Samplesort: Animated example of Super Scalar Samplesort. In each step, numbers that are compared are marked blue and numbers that are otherwise read or written are marked red.
Animated example of Super Scalar Samplesort. In each step, numbers that are compared are marked blue and numbers that are otherwise read or written are marked red.

Worked examples

Example 1 — a first encounter with Samplesort

Start with the simplest possible case. Write down what Samplesort 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 Samplesort 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 Samplesort 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 Samplesort

In research
Samplesort 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 Samplesort 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
Samplesort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Distributed algorithms, Sorting algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Samplesort 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.

Affiliate

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

How to study Samplesort in 20 minutes

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

Frequently asked questions

What is Samplesort in simple terms?

Samplesort is a sorting algorithm that is a divide and conquer algorithm often used in parallel processing systems. Conventional divide and conquer sorting algorithms partitions the array into sub-intervals or buckets.

Why does Samplesort 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 Samplesort?

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 Samplesort.

Tags

  • Distributed algorithms
  • Sorting algorithms

Keep exploring