This module will create sorting networks, a sequence of comparisons that do not depend upon the results of prior comparisons.

Since the sequences and their order never change, they can be very useful if deployed in hardware, or if used in software with a compiler that can take advantage of parallelism. Unfortunately a sorting network cannot be used for generic run-time sorting like quicksort, since the arrangement of the comparisons is fixed according to the number of elements to be sorted.

This module's main purpose is to create compare-and-swap macros (or functions, or templates) that one may insert into source code. It may also be used to create images of the sorting networks in either encapsulated postscript (EPS), scalar vector graphics (SVG), or in "ascii art" format.

Return a descriptive title for the network, given an algorithm's key name.

$title = nwsrt_title($key);

These are the titles for the available algorithms. By themselves, they provide a readable list of choices for an interactive program. They are not to be confused with a sorting network's title, which may be set by the programmer.

Returns an object that contains, among other things, a list of comparators that can sort $inputs items. The algorithm for generating the list may be chosen, but by default the sorting network is generated by the Bose-Nelson algorithm.

Use Hibbard's algorithm. This iterative algorithm was developed after the Bose-Nelson algorithm was published, and produces a different network "... for generating the comparisons one by one in the order in which they are needed for sorting," according to his article (see below).

Use Batcher's Merge Exchange algorithm. Merge Exchange is a real sort, in that in its usual form (for example, as described in Knuth) it can handle a variety of inputs. But while sorting it always generates an identical set of comparison pairs per array size, which lends itself to sorting networks.

Use Batcher's bitonic algorithm. A bitonic sequence is a sequence that monotonically increases and then monotonically decreases. The bitonic sort algorithm works by recursively splitting sequences into bitonic sequences using so-called "half-cleaners". These bitonic sequences are then merged into a fully sorted sequence. Bitonic sort is a very efficient sort and is especially suited for implementations that can exploit network parallelism.

Use Batcher's Odd-Even Merge algorithm. This sort works in a similar way to a regular merge sort, except that in the merge phase the sorted halves are merged by comparing even elements separately from odd elements. This algorithm creates very efficient networks in both comparators and stages.

Use a naive odd-even transposition sort. This is a primitive sort closely related to bubble sort except it is more parallel. Because other algorithms are more efficient, this sort is included for illustrative purposes.

This network is described in the 1983 paper "The Balanced Sorting Network" by M. Dowd, Y. Perl, M Saks, and L. Rudolph. It is not a particularly efficient sort but it has some interesting properties due to the fact that it is constructed as a series of successive identical sub-blocks, somewhat like with 'oddeventrans'.

Sort an array using the network. This is meant for testing purposes only - looping around an array of comparators in order to sort an array in an interpreted language is not the most efficient mechanism for using a sorting network.

The values to use to reference array indices in formatted printing (see formatted()). By default, array indices are zero-based. To use a different index base (most commonly, one-based array indexing), use this method.

Takes the comparator list and returns a list of comparator lists, each sub-list representing a group of comparators that can be operate without interfering with each other, depending on what is needed for interference-free grouping.

There is one option available, 'grouping', that will produce a grouping that represents parallel operations of comparators. Its values may be:

All parts not named are reset to 'undef', and will be colored with the default 'foreground' color. The foreground color itself has a default value of 'black'. The one exception is the 'background' color, which has no default color at all.

Default value: 9. The indention between the start of the input lines and the placement of the first comparator. The same value spaces the placement of the final comparator and the end of the input lines.

Kenneth Batcher, "Sorting Networks and their Applications", Proc. of the AFIPS Spring Joint Computing Conf., Vol. 32, 1968, pp. 307-3114. A PDF of this article may be found at http://www.cs.kent.edu/~batcher/sort.pdf.

The paper discusses both the Odd-Even Merge algorithm and the Bitonic algorithm.