Sorting library
Olin Shivers (edited by John Cowan)
This SRFI describes the API for a full-featured sort toolkit. The spec comes with 1200 lines of high-quality reference code: tightly written, highly commented, portable code, available for free.
This SRFI is an edited version of SRFI 32 by Olin Shivers, which was withdrawn twelve years ago.
There are only two changes to the API. The most important one is that the comparison predicate now precedes the data rather than following it. Most of the SRFI 32 commenters thought that this is the better order, despite the widespread precedent. The second point is that the algorithm-specific procedures have been removed from the public API, though not from the sample implementation.
Editorially, the text has been reorganized, removing the massive redundancies. In accordance with R5RS and R7RS, procedures that in SRFI 32 returned zero or more unspecified values now return one value. The multiple packages, most with only a few procedures, into which SRFI 32 is divided have been removed, and references to SRFI 95 and R6RS equivalents added.
In SRFI 32 there were two different interfaces: "what" (simple) and "how" (detailed).
However, the difficulty with exposing specific algorithms by name is that the local optima in the search space of algorithms changes over time. For example, the "qsort" in the musl C library is actually smooth sort, not quick sort; Python and Java have switched from quick sort to timsort; some implementations of the C++ STL use introsort rather than quicksort. Having procedure names that imply e.g. quick sort, that do not actually implement quick sort, is confusing.
Therefore, only the "what" interface has been retained. The sample implementation uses the original SRFI 32 algorithms, but this is not a requirement of this SRFI.
Procedures share common signatures wherever possible, to facilitate switching a given call from one procedure to another.
These procedures uniformly observe the following parameter order: the comparison predicate comes before the data to be sorted. In SRFI 32, the reverse convention was used, because it was consistent with all the implementations examined in 2002, with the sole exception of Chez Scheme. However, R6RS adopted the reverse convention, which is more consistent with other Scheme libraries that put the procedure first — the "operation currying" convention, as in for-each or map or find. This SRFI uses the R6RS order throughout.
A "stable" sort is one that preserves the pre-existing order of equal elements. Suppose, for example, that we sort a list of numbers by comparing their absolute values using abs< (defined below). If we sort a list that contains both 3 and -3, then a stable sort is an algorithm that will not swap the order of these two elements, that is, the answer will look like ... 3 -3 ..., not ... -3 3 ....
A "stable" merge is one that reliably favors one of its data sets when equal items appear in both data sets. All merge operations in this library are stable, breaking ties between data sets in favor of the first data set — elements of the first list come before equal elements in the second list.
So, if we are merging two lists of numbers ordered by absolute value using the stable merge operation list-merge and abs< compares numbers by their absolute values, then
(list-merge abs< '(0 -2 4 8 -10) '(-1 3 -4 7))reliably places the 4 of the first list before the equal-comparing -4 of the second list: (0 -1 -2 4 -4 7 8 -10).
Here's a definition of abs<, introduced just for the examples (it is not part of this SRFI or any SRFI):
(define (abs< x y) (< (abs x) (abs y))))The vector operations specified below all take optional start and end arguments indicating a selected subrange of a vector's elements.
list-sort! and list-stable-sort! are allowed, but not required, to alter their arguments' cons cells to construct the result list. This is consistent with the what-not-how character of the group of procedures to which they belong.
The list-delete-neighbor-dups!, list-merge! and list-sort! procedures, on the other hand, provide specific algorithms, and, as such, explicitly commit to the use of side-effects on their input lists in order to guarantee their key algorithmic properties (e.g., linear-time operation, constant-space stack use).
Note that sorting lists involves chasing pointers through memory, which can be a loser on modern machine architectures because of poor cache and page locality. Pointer writing, which is what the set-cdr!s of a destructive list-sort algorithm do, is even worse, especially if your Scheme has a generational GC — the writes will thrash the write-barrier. Sorting vectors has inherently better locality.
The reference implementation's destructive list merge and merge sort implementations are opportunistic — they avoid redundant set-cdr!s, and try to take long already-ordered runs of list structure as-is when doing the merges.
Most of the procedures described below are variants of two basic operations: sorting and merging. These procedures are consistently named by composing a set of basic lexemes to indicate what they do.
||Lexeme||Meaning
vector |
The procedure operates upon vectors. |
list |
The procedure operates upon lists. |
stable |
This lexeme indicates that the sort is a stable one. |
sort |
The procedure sorts its input data set by some ordering function. |
merge |
The procedure merges two ordered data sets into a single ordered result. |
! |
Procedures that end in ! are allowed, and sometimes required, to reuse their input storage to construct their answer. |
In the procedures specified below,
Passing values to procedures with these parameters that do not satisfy these types is an error.
If a procedure is said to return "an unspecified value", this means that nothing at all is said about what the procedure returns, except that it returns one value.
(list-sorted? < lis)
(vector-sorted? < v [start [ end ] ]
These procedures return true iff their input list or vector is in sorted order, as determined by <. Specifically, they return #f iff there is an adjacent pair ... X Y ... in the input list or vector such that Y < X in the sense of <. The optional start and end range arguments restrict vector-sorted? to examining the indicated subvector.
These procedures are equivalent to the SRFI 95 sorted? procedure when applied to lists or vectors respectively, except that they do not accept a key procedure.
These procedures provide basic sorting and merging functionality suitable for general programming. The procedures are named by their semantic properties,
(list-sort < lis)
(list-stable-sort < lis)
These procedures do not alter their inputs and are allowed to return a value that shares a common tail with a list argument.
The list-stable-sort procedure is equivalent to the R6RS list-sort procedure. It is also equivalent to the SRFI 95 sort procedure when applied to lists, except that it does not accept a key procedure.
(list-sort! < lis)
(list-stable-sort! < lis)
These procedures are "linear update" operators — they are allowed, but not required, to alter the cons cells of their arguments to produce their results.
The list-stable-sort! procedure is equivalent to the SRFI 95 sort! procedure when applied to lists, except that it does not accept a key procedure.
(vector-sort < v [ start [ end ] ])
(vector-stable-sort < v [ start [ end ] ])
These procedures do not alter their inputs, but allocate a fresh vector for their result, of length end - start. The vector-stable-sort procedure with no optional arguments is equivalent to the R6RS vector-sort procedure. It is also equivalent to the SRFI 95 sort procedure when applied to vectors, except that it does not accept a key procedure.
(vector-sort! < v [ start [ end ] ])
(vector-stable-sort! < v [ start [ end ] ])
These procedures sort their data in-place. (But note that vector-stable-sort! may allocate temporary storage proportional to the size of the input — there are no known O(n lg n) stable vector sorting algorithms that run in constant space.) They return an unspecified value.
The vector-sort! procedure with no optional arguments is equivalent to the R6RS vector-sort! procedure.
All four merge operations are stable: an element of the initial list lis1 or vector v1 will come before an equal-comparing element in the second list lis2 or vector v2 in the result.
(list-merge < lis1 lis2)
This procedure does not alter its inputs, and is allowed to return a value that shares a common tail with a list argument.
This procedure is equivalent to the SRFI 95 merge procedure when applied to lists, except that it does not accept a key procedure.
(list-merge! < lis1 lis2)
This procedure makes only a single, iterative, linear-time pass over its argument lists, using set-cdr!s to rearrange the cells of the lists into the list that is returned — they work "in place." Hence, any cons cell appearing in the result must have originally appeared in an input.
Additionally, list-merge! is iterative, not recursive — it can operate on arguments of arbitrary size without requiring an unbounded amount of stack space. The intent of this iterative-algorithm commitment is to allow the programmer to be sure that if, for example, list-merge! is asked to merge two ten-million-element lists, the operation will complete without performing some extremely (possibly twenty-million) deep recursion.
This procedure is equivalent to the SRFI 95 merge! procedure when applied to lists, except that it does not accept a key procedure.
(vector-merge < v1 v2 [ start1 [ end1 [ start2 [ end2 ] ] ] ])
This procedure does not alter its inputs, and returns a newly allocated vector of length (end1 - start1) + (end2 - start2).
This procedure is equivalent to the SRFI 95 merge procedure when applied to vectors, except that it does not accept a key procedure.
(vector-merge! < to from1 from2 [ start [ start1 [ end1 [ start2 [ end2 ] ] ] ] ])
This procedure writes its result into vector to, beginning at index start, for indices less than end, which is defined as start + (end1 - start1) + (end2 - start2). The target subvector to[start, end) may not overlap either of the source subvectors from1[start1, end1] and from2[start2, end2]. It returns an unspecified value.
This procedure is equivalent to the SRFI 95 merge! procedure when applied to lists, except that it does not accept a key procedure.
These procedures delete adjacent duplicate elements from a list or a vector, using a given element-equality procedure. The first/leftmost element of a run of equal elements is the one that survives. The list or vector is not otherwise disordered.
These procedures are linear time — much faster than the O(n2) general duplicate-element deletion procedures that do not assume any "bunching" of elements (such as the ones provided by SRFI 1). If you want to delete duplicate elements from a large list or vector, sort the elements to bring equal items together, then use one of these procedures, for a total time of O(n lg n).
The equality procedure extracted from the < passed to these procedures is always applied as (= x y), where x comes before y in the containing list or vector.
(list-delete-neighbor-dups = lis)
This procedure does not alter its input list; its result may share storage with the input list.
(list-delete-neighbor-dups! = lis)
This procedure mutates its input list in order to construct its result. It makes only a single, iterative, linear-time pass over its argument, using set-cdr!s to rearrange the cells of the list into the final result — it works "in place." Hence, any cons cell appearing in the result must have originally appeared in the input.
(vector-delete-neighbor-dups = v [ start [ end ] ])
This procedure does not alter its input vector, but rather newly allocates and returns a vector to hold the result.
(vector-delete-neighbor-dups! = v [ start [ end ] ])
This procedure reuses its input vector to hold the answer, packing its answer into the index range [start,newend), where newend is the non-negative exact integer that is returned as its value. The vector is not altered outside the range [start, newend).
Examples:
(list-delete-neighbor-dups = '(1 1 2 7 7 7 0 -2 -2)) => (1 2 7 0 -2) (vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2)) => #(1 2 7 0 -2) (vector-delete-neighbor-dups < '#(1 1 2 7 7 7 0 -2 -2) 3 7)) => #(7 0 -2) ;; Result left in v[3,9): (let ((v (vector 0 0 0 1 1 2 2 3 3 4 4 5 5 6 6))) (cons (vector-delete-neighbor-dups! < v 3) v)) => (9 . #(0 0 0 1 2 3 4 5 6 4 4 5 5 6 6))The sample implementation is the Scheme 48 implementation of the sorting structure. This is close to the original SRFI 32 reference implementation, but includes some bug fixes and switches the < and = arguments to the initial position. The code is very portable and freely reusable. It is tightly bummed, as far as could be done in portable Scheme, and is commented in Olin's usual voluminous style, including notes on porting and implementation-specific optimizations. It should be trivial to port.
The following files are included:
delndups.scm - the delete-neighbor-dups functions lmsort.scm - merge sort sort.scm - generic sort and merge functions sorting-test.scm - test file using the Chicken test egg sorting.scm - a Chicken library providing this SRFI and additional functions sorting.sld - an R7RS counterpart of sorting.scm sortp.scm - sort predicates srfi-132.scm - a Chicken library providing this SRFI only srfi-132.sld - an R7RS counterpart of srfi-132.scm vbinsearch.scm - binary search routines for sorting library vector-util.scm - vector utilities vhsort.scm - vector heap sort visort.scm - vector insert sort vmsort.scm - vector merge sort vqsort2.scm - vector quick sort vqsort3.scm - vector quick sort using comparison functions
The sorting libraries include the following additional functions: (vector-binary-search key< elt->key key v [ start [ end ]]) vector-binary-search3 compare v [ start [ end ]])) (vector-quick-sort3! comp v [ start [ end ]])) (vector-quick-sort3 comp v [ start [ end ]]))
The natural merge sorts (pure list, destructive list, and vector) are not only Olin's code, but are implementations of an algorithm of his own devising. They run in O(n lg n) worst case, O(n) best case, and require only a logarithmic number of stack frames. And they are stable. And the destructive-list variant allocates zero cons cells; it simply rearranges the cells of the input list.
The only non-R4RS features in the code are the use of R5RS/R6RS/R7RS multiple-value return, with values and call-with-values procedures, and the use of R7RS-style error to report an assertion violation.
You could speed up the vector code a lot by error-checking the procedure parameters and then shifting over to fixnum-specific arithmetic and dangerous vector-indexing and vector-setting primitives. The comments in the code indicate where the initial error checks would have to be added. There are several (quotient n 2) calls that could be changed to a fixnum right-shift, as well, in both the list and vector code. The code is designed to enable this — each file usually exports one or two "safe" procedures that end up calling an internal "dangerous" primitive. The little exported cover procedures are where you move the error checks.
This should provide big speedups. In fact, all the code bumming in the source pretty much disappears in the noise unless you have a good compiler and also can dump the vector-index checks and generic arithmetic — so it's really set up for optimization rather than fully optimized.
The optional-arg parsing, defaulting, and error checking is done with a portable R4RS macro. But if the target Scheme has a faster mechanism (e.g., Chez), it's definitely better to switch to using it. Note that argument defaulting and error-checking are interleaved — there's no need to error-check defaulted start and end args to see if they are fixnums that are legal vector indices for the corresponding vector, etc.
Olin thanks the authors of the open source consulted when designing this library, particularly Richard O'Keefe, Donovan Kolbly and the MIT Scheme Team.
This document is copyright (C) Olin Shivers (1998, 1999). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Short summary: no restrictions.
While Olin wrote all of this code himself, he read a lot of code before he began writing. However, all such code is, itself, either open source or public domain, rendering irrelevant any issue of "copyright taint."
Hence the sample implementation is Copyright © 1998 by Olin Shivers and made available under the same copyright as the SRFI text (see above).