BREAKING NEWS
Funnelsort

## Summary

(Learn how and when to remove this template message)

We 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.[1][2]

## Mathematical properties

In the external memory model, the number of memory transfers it needs to perform a sort of ${\displaystyle N}$  items on a machine with cache of size ${\displaystyle Z}$  and cache lines of length ${\displaystyle L}$  is ${\displaystyle O\left({\tfrac {N}{L}}\log _{Z}N\right)}$ , under the tall cache assumption that ${\displaystyle Z=\Omega (L^{2})}$ . This number of memory transfers has been shown to be asymptotically optimal for comparison sorts. Funnelsort also achieves the asymptotically optimal runtime complexity of ${\displaystyle \Theta (N\log N)}$ .

## Algorithm

### Basic overview

Funnelsort operates on a contiguous array of ${\displaystyle N}$  elements. To sort the elements, it performs the following:

1. Split the input into ${\displaystyle N^{1/3}}$  arrays of size ${\displaystyle N^{2/3}}$ , and sort the arrays recursively.
2. Merge the ${\displaystyle N^{1/3}}$  sorted sequences using a ${\displaystyle N^{1/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 ${\displaystyle k}$  sorted sequences. Upon one invocation of a k-merger, it outputs the first ${\displaystyle k^{3}}$  elements of the sorted sequence obtained by merging the k input sequences.

At the top level, funnelsort uses a ${\displaystyle N^{1/3}}$ -merger on ${\displaystyle N^{1/3}}$  sequences of length ${\displaystyle N^{2/3}}$ , and invokes this merger once.

The k-merger is built recursively out of ${\displaystyle {\sqrt {k}}}$ -mergers. It consists of ${\displaystyle {\sqrt {k}}}$  input ${\displaystyle {\sqrt {k}}}$ -mergers ${\displaystyle I_{1},I_{2},\ldots ,I_{\sqrt {k}}}$ , and a single output ${\displaystyle {\sqrt {k}}}$ -merger ${\displaystyle O}$ . The k inputs are separated into ${\displaystyle {\sqrt {k}}}$  sets of ${\displaystyle {\sqrt {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 ${\displaystyle 2k^{3/2}}$  elements. The buffers are implemented as circular queues. The outputs of the ${\displaystyle {\sqrt {k}}}$  buffers are connected to the inputs of the output merger ${\displaystyle O}$ . Finally, the output of ${\displaystyle O}$  is the output of the entire k-merger.

In this construction, any input merger only outputs ${\displaystyle k^{3/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, ${\displaystyle k^{3/2}}$  of them).

A k-merger works recursively in the following way. To output ${\displaystyle k^{3}}$  elements, it recursively invokes its output merger ${\displaystyle k^{3/2}}$  times. However, before it makes a call to ${\displaystyle 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 ${\displaystyle I_{i}}$  once. If this cannot be done (due to the merger running out of inputs), this step is skipped. Since this call outputs ${\displaystyle k^{3/2}}$  elements, the buffer contains at least ${\displaystyle k^{3/2}}$  elements. At the end of all these operations, the k-merger has output the first ${\displaystyle k^{3}}$  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 ${\displaystyle O(k^{2})}$  space. To see this, we let ${\displaystyle S(k)}$  denote the space needed for a k-merger. To fit the ${\displaystyle k^{1/2}}$  buffers of size ${\displaystyle 2k^{3/2}}$  takes ${\displaystyle O(k^{2})}$  space. To fit the ${\displaystyle {\sqrt {k}}+1}$  smaller buffers takes ${\displaystyle ({\sqrt {k}}+1)S({\sqrt {k}})}$  space. Thus, the space satisfies the recurrence ${\displaystyle S(k)=({\sqrt {k}}+1)S({\sqrt {k}})+O(k^{2})}$ . This recurrence has solution ${\displaystyle S(k)=O(k^{2})}$ .

It follows that there is a positive constant ${\displaystyle \alpha }$  such that a problem of size at most ${\displaystyle \alpha {\sqrt {Z}}}$  fits entirely in cache, meaning that it incurs no additional cache misses.

Letting ${\displaystyle Q_{M}(k)}$  denote the number of cache misses incurred by a call to a k-merger, one can show that ${\displaystyle Q_{M}(k)=O((k^{3}\log _{Z}k)/L).}$  This is done by an induction argument. It has ${\displaystyle k\leq \alpha {\sqrt {Z}}}$  as a base case. For larger k, we can bound the number of times a ${\displaystyle {\sqrt {k}}}$ -merger is called. The output merger is called exactly ${\displaystyle k^{3/2}}$  times. The total number of calls on input mergers is at most ${\displaystyle k^{3/2}+2{\sqrt {k}}}$ . This gives a total bound of ${\displaystyle 2k^{3/2}+2{\sqrt {k}}}$  recursive calls. In addition, the algorithm checks every buffer to see if needs to be filled. This is done on ${\displaystyle {\sqrt {k}}}$  buffers every step for ${\displaystyle k^{3/2}}$  steps, leading to a max of ${\displaystyle k^{2}}$  cache misses for all the checks.

This leads to the recurrence ${\displaystyle Q_{M}(k)\leq (2k^{3/2}+2{\sqrt {k}})Q_{M}({\sqrt {k}})+k^{2}}$ , which can be shown to have the solution given above.

Finally, the total cache misses ${\displaystyle Q(N)}$  for the entire sort can be analyzed. It satisfies the recurrence ${\displaystyle Q(N)=N^{1/3}Q(N^{2/3})+Q_{M}(N^{1/3}).}$  This can be shown to have solution ${\displaystyle Q(N)=O((N/L)\log _{Z}N).}$

## Lazy funnelsort

Lazy funnelsort is a modification of the funnelsort, introduced by Gerth Stølting Brodal and Rolf Fagerberg in 2002.[3] 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.