ArticleslgStudy

computer science

Funnelsort

Funnelsort 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 Funnelsort rather than just read about it. In short: Funnelsort is a comparison-based sorting algorithm. It is similar to mergesort, but it is a cache-oblivious algorithm, designed for a setting where the number of elements to sort is too large to fit in a cache where operations are done.

Key takeaways

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

Reference excerpt

Funnelsort is a comparison-based sorting algorithm. It is similar to mergesort, but it is a cache-oblivious algorithm, designed for a setting where the number of elements to sort is too large to fit in a cache where operations are done. It was introduced by Matteo Frigo, Charles Leiserson, Harald Prokop, and Sridhar Ramachandran in 1999 in the context of the cache oblivious model.

Mathematical properties In the external memory model, the number of memory transfers it needs to perform a sort of N items on a machine with cache of size Z and cache lines of length L is O(NLlogZN), under the tall cache assumption that Z=Ω(L2). This number of memory transfers has been shown to be asymptotically optimal for comparison sorts. Funnelsort also achieves the asymptotically optimal runtime complexity of Θ(Nlog⁡N).

Algorithm

Basic overview Funnelsort operates on a contiguous array of N elements. To sort the elements, it performs the following:

Split the input into N1/3 arrays of size N2/3, and sort the arrays recursively. Merge the N1/3 sorted sequences using a N1/3-merger. (This process will be described in more detail.) Funnelsort is similar to merge sort in that some number of subarrays are recursively sorted, after which a merging step combines the subarrays into one sorted array. Merging is performed by a device called a k-merger, which is described in the section below.

k-mergers A k-merger takes k sorted sequences. Upon one invocation of a k-merger, it outputs the first k3 elements of the sorted sequence obtained by merging the k input sequences. At the top level, funnelsort uses a N1/3-merger on N1/3 sequences of length N2/3, and invokes this merger once. The k-merger is built recursively out of k-mergers. It consists of k input k-mergers I1,I2,…,Ik, and a single output k-merger O. The k inputs are separated into k sets of k inputs each. Each of these sets is an input to one of the input mergers. The output of each input merger is connected to a buffer, a FIFO queue that can hold 2k3/2 elements. The buffers are implemented as circular queues. The outputs of the k buffers are connected to the inputs of the output merger O. Finally, the output of O is the output of the entire k-merger. In this construction, any input merger only outputs k3/2 items at once, but the buffer it outputs to has double the space. This is done so that an input merger can be called only when its buffer does not have enough items, but that when it is called, it outputs a lot of items at once (namely, k3/2 of them). A k-merger works recursively in the following way. To output k3 elements, it recursively invokes its output merger k3/2 times. However, before it makes a call to O, it checks all of its buffers, filling each of them that are less than half full. To fill the i-th buffer, it recursively invokes the corresponding input merger Ii once. If this cannot be done (due to the merger running out of inputs), this step is skipped. Since this call outputs k3/2 elements, the buffer contains at least k3/2 elements. At the end of all these operations, the k-merger has output the first k3 of its input elements, in sorted order.

Analysis Most of the analysis of this algorithm revolves around analyzing the space and cache miss complexity of the k-merger. The first important bound is that a k-merger can be fit in O(k2) space. To see this, we let S(k) denote the space needed for a k-merger. To fit the k1/2 buffers of size 2k3/2 takes O(k2) space. To fit the k+1 smaller buffers takes (k+1)S(k) space. Thus, the space satisfies the recurrence S(k)=(k+1)S(k)+O(k2). This recurrence has solution S(k)=O(k2). It follows that there is a positive constant α such that a problem of size at most αZ fits entirely in cache, meaning that it incurs no additional cache misses. Letting QM(k) denote the number of cache misses incurred by a call to a k-merger, one can show that QM(k)=O((k3logZk)/L). This is done by an induction argument. It has k≤αZ as a base case. For larger k, we can bound the number of times a k-merger is called. The output merger is called exactly k3/2 times. The total number of calls on input mergers is at most k3/2+2k. This gives a total bound of 2k3/2+2k recursive calls. In addition, the algorithm checks every buffer to see if needs to be filled. This is done on k buffers every step for k3/2 steps, leading to a max of k2 cache misses for all the checks. This leads to the recurrence QM(k)≤(2k3/2+2k)QM(k)+k2, which can be shown to have the solution given above. Finally, the total cache misses Q(N) for the entire sort can be analyzed. It satisfies the recurrence Q(N)=N1/3Q(N2/3)+QM(N1/3). This can be shown to have solution Q(N)=O((N/L)logZN).

Lazy funnelsort Lazy funnelsort is a modification of the funnelsort, introduced by Gerth Stølting Brodal and Rolf Fagerberg in 2002. The modification is that when a merger is invoked, it does not have to fill each of its buffers. Instead, it lazily fills a buffer only when it is empty. This modification has the same asymptotic runtime and memory transfers as the original funnelsort, but has applications in cache-oblivious algorithms for problems in computational geometry in a method known as distribution sweeping.

See also Cache-oblivious algorithm Cache-oblivious distribution sort External sorting

References

Worked examples

Example 1 — a first encounter with Funnelsort

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

In research
Funnelsort 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 Funnelsort 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
Funnelsort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Analysis of algorithms, Cache (computing), Comparison sorts, so understanding it makes those chapters shorter.
In everyday life
Look for Funnelsort 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 “Funnelsort” →

How to study Funnelsort in 20 minutes

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

Frequently asked questions

What is Funnelsort in simple terms?

Funnelsort is a comparison-based sorting algorithm. It is similar to mergesort, but it is a cache-oblivious algorithm, designed for a setting where the number of elements to sort is too large to fit in a cache where operations are done.

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

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

Tags

  • Analysis of algorithms
  • Cache (computing)
  • Comparison sorts
  • External memory algorithms
  • Models of computation

Keep exploring