1 | == SRFI-95: Sorting and Merging |
---|
2 | === Abstract |
---|
3 | Sorting and Merging are useful operations deserving a common API. |
---|
4 | |
---|
5 | For more information, see: [[https://srfi.schemers.org/srfi-95/|SRFI-95: Sorting and Merging]] |
---|
6 | === Issues |
---|
7 | * Changed so that the algorithms are required to call key arguments no more than once per element. As a consequence, the pair allocation constraints for {{sort!}} and {{merge!}} are removed. |
---|
8 | |
---|
9 | * These procedures are stable only for {{less?}} predicates which return {{#f}} when applied to identical arguments. With non-empty sequence arguments, {{less?}} can easily be tested. Should these procedures signal an error when given reflexive predicates? Should they silently replace {{less?}} with |
---|
10 | |
---|
11 | <enscript highlight="scheme"> |
---|
12 | (lambda (a b) (not (less? b a))) |
---|
13 | </enscript> |
---|
14 | === Rationale |
---|
15 | General purpose software libraries are about simplicity and ease of use, not theoretical perfection in algorithm design. A sorting library should be specified so that its routines will perform moderately well for moderately sized inputs in the vast majority of applications. |
---|
16 | |
---|
17 | When [[https://srfi.schemers.org/srfi-32/srfi-32.html|SRFI 32 Sort Libraries]] was withdrawn, it had 28 procedures. Having many variants in a general-purpose sorting library has disadvantages: |
---|
18 | |
---|
19 | * When there are only a few paths through the code, the code gets thoroughly tested and its behavior well understood. When there are many paths, most of the code is not well tested and not well understood. |
---|
20 | |
---|
21 | * To choose optimal sort algorithms requires nearly as much understanding as to write them. Most users don't. |
---|
22 | |
---|
23 | * A module with too many functions and voluminous documentation scares off the typical user looking to just sort a 50-element list; who then goes searching for any old sort algorithm to reinvent the wheel. |
---|
24 | |
---|
25 | * If some of the default sorts are unstable, then users will be surprised that sorting their data twice results in different orders; or that vector and list sorts return different orders. |
---|
26 | |
---|
27 | The table in Wikipedia [[http://en.wikipedia.org/wiki/Sorting_algorithm|Sorting algorithm]], shows that the merge-sort class of algorithms is optimal in terms of space and time asymptotic behavior except for the best case time, which is obtained at the expense of making the sort unstable. |
---|
28 | |
---|
29 | This SRFI's sort procedures operate on lists and arrays, which includes vectors; the {{merge}} procedures operate on lists. |
---|
30 | |
---|
31 | [[https://srfi.schemers.org/srfi-32/srfi-32.html|SRFI 32]]'s vector routines took optional arguments to restrict their operations to a subrange of the vector. [[https://srfi.schemers.org/srfi-63/srfi-63.html|SRFI 63]] shared subarrays (using make-shared-array or [[http://swiss.csail.mit.edu/~jaffer/SLIB|SLIB]]'s |
---|
32 | subarray) eliminate the need for these optional arguments. |
---|
33 | |
---|
34 | The present SRFI procedures take an optional procedure argument equivalent to Common-Lisp's {{&KEY}} argument. |
---|
35 | === Specification |
---|
36 | These procedures are stable when called with predicates which return {{#f}} when applied to identical arguments. |
---|
37 | |
---|
38 | The {{sorted?}}, {{merge}}, and {{merge!}} procedures consume asymptotic time and space no larger than {{O(N)}}, where {{N}} is the sum of the lengths of the sequence arguments. The sort and {{sort!}} procedures consume asymptotic time and space no larger than {{O(N*log(N))}}, where {{N}} is the length of the sequence argument. |
---|
39 | |
---|
40 | All five functions take an optional key argument corresponding to a CL-style {{`&key'}} argument. A {{less?}} predicate with a key argument behaves like: |
---|
41 | |
---|
42 | <enscript highlight="scheme"> |
---|
43 | (lambda (x y) (less? (key x) (key y))) |
---|
44 | </enscript> |
---|
45 | |
---|
46 | All five functions will call the key argument at most once per element. |
---|
47 | |
---|
48 | The {{`!'}} variants sort in place; {{sort!}} returns its sequence argument. |
---|
49 | |
---|
50 | <procedure>sorted? sequence less?</procedure> |
---|
51 | |
---|
52 | |
---|
53 | <procedure>sorted? sequence less? key</procedure> |
---|
54 | |
---|
55 | Returns {{#t}} when the sequence argument is in non-decreasing order according to {{less?}} (that is, there is no adjacent pair {{... x y ...}} for which {{(less? y x))}}. |
---|
56 | |
---|
57 | Returns {{#f}} when the sequence contains at least one out-of-order pair. It is an error if the sequence is not a list or array (including vectors and strings). |
---|
58 | |
---|
59 | <procedure>merge list1 list2 less?</procedure> |
---|
60 | |
---|
61 | |
---|
62 | <procedure>merge list1 list2 less? key</procedure> |
---|
63 | |
---|
64 | Merges two sorted lists, returning a freshly allocated list as its result. |
---|
65 | |
---|
66 | <procedure>merge! list1 list2 less?</procedure> |
---|
67 | |
---|
68 | |
---|
69 | <procedure>merge! list1 list2 less? key</procedure> |
---|
70 | |
---|
71 | Merges two sorted lists, re-using the pairs of {{list1}} and {{list2}} to build the result. The result will be either {{list1}} or {{list2}}. |
---|
72 | |
---|
73 | <procedure>sort sequence less?</procedure> |
---|
74 | |
---|
75 | |
---|
76 | <procedure>sort sequence less? key</procedure> |
---|
77 | |
---|
78 | Accepts a list or array (including vectors and strings) for sequence; and returns a completely new sequence which is sorted according to {{less?}}. The returned sequence is the same type as the argument sequence. |
---|
79 | ===== Given valid arguments, it is always the case that: |
---|
80 | <enscript highlight="scheme"> |
---|
81 | (sorted? (sort sequence less?) less?) => #t |
---|
82 | </enscript> |
---|
83 | |
---|
84 | <procedure>sort! sequence less?</procedure> |
---|
85 | |
---|
86 | |
---|
87 | <procedure>sort! sequence less? key</procedure> |
---|
88 | |
---|
89 | Returns list, array, vector, or string sequence which has been mutated to order its elements according to {{less?}}. Given valid arguments, it is always the case that: |
---|
90 | |
---|
91 | <enscript highlight="scheme"> |
---|
92 | (sorted? (sort! sequence less?) less?) => #t |
---|
93 | </enscript> |
---|
94 | === Author |
---|
95 | * Aubrey Jaffer |
---|
96 | * Ported to hygienic Chicken with test suite by Peter Danenberg |
---|
97 | * Ported to Chicken 5 by Sergey Goldgaber |
---|
98 | === Copyright |
---|
99 | Copyright (C) Aubrey Jaffer 2006. All Rights Reserved. |
---|
100 | |
---|
101 | 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: |
---|
102 | |
---|
103 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
---|
104 | |
---|
105 | 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. |
---|
106 | === Version history |
---|
107 | * [[https://github.com/diamond-lizard/srfi-95/releases/tag/0.1|2.0]] - Ported to Chicken Scheme 5 |
---|
108 | |
---|