1.\" $NetBSD: radixsort.3,v 1.11 2002/02/07 07:00:30 ross Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94 35.\" 36.Dd January 27, 1994 37.Dt RADIXSORT 3 38.Os 39.Sh NAME 40.Nm radixsort , 41.Nm sradixsort 42.Nd radix sort 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.Fd #include \*[Lt]limits.h\*[Gt] 47.Fd #include \*[Lt]stdlib.h\*[Gt] 48.Ft int 49.Fn radixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 50.Ft int 51.Fn sradixsort "const u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 52.Sh DESCRIPTION 53The 54.Fn radixsort 55and 56.Fn sradixsort 57functions 58are implementations of radix sort. 59.Pp 60These functions sort an 61.Fa nmemb 62element array of pointers to byte strings, with 63the initial member of which is referenced by 64.Fa base . 65The byte strings may contain any values. 66End of strings is denoted 67by character which has same weight as user specified value 68.Fa endbyte . 69.Fa endbyte 70has to be between 0 and 255. 71.Pp 72Applications may specify a sort order by providing the 73.Fa table 74argument. 75If 76.Pf non- Dv NULL , 77.Fa table 78must reference an array of 79.Dv UCHAR_MAX 80+ 1 bytes which contains the sort 81weight of each possible byte value. 82The end-of-string byte must have a sort weight of 0 or 255 83(for sorting in reverse order). 84More than one byte may have the same sort weight. 85The 86.Fa table 87argument 88is useful for applications which wish to sort different characters 89equally, for example, providing a table with the same weights 90for A-Z as for a-z will result in a case-insensitive sort. 91If 92.Fa table 93is NULL, the contents of the array are sorted in ascending order 94according to the 95.Tn ASCII 96order of the byte strings they reference and 97.Fa endbyte 98has a sorting weight of 0. 99.Pp 100The 101.Fn sradixsort 102function is stable, that is, if two elements compare as equal, their 103order in the sorted array is unchanged. 104The 105.Fn sradixsort 106function uses additional memory sufficient to hold 107.Fa nmemb 108pointers. 109.Pp 110The 111.Fn radixsort 112function is not stable, but uses no additional memory. 113.Pp 114These functions are variants of most-significant-byte radix sorting; in 115particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. 116They take linear time relative to the number of bytes in the strings. 117.Sh RETURN VALUES 118Upon successful completion 0 is returned. 119Otherwise, \-1 is returned and the global variable 120.Va errno 121is set to indicate the error. 122.Sh ERRORS 123.Bl -tag -width Er 124.It Bq Er EINVAL 125The value of the 126.Fa endbyte 127element of 128.Fa table 129is not 0 or 255. 130.El 131.Pp 132Additionally, the 133.Fn sradixsort 134function 135may fail and set 136.Va errno 137for any of the errors specified for the library routine 138.Xr malloc 3 . 139.Sh SEE ALSO 140.Xr sort 1 , 141.Xr qsort 3 142.Pp 143.Rs 144.%A Knuth, D.E. 145.%D 1968 146.%B "The Art of Computer Programming" 147.%T "Sorting and Searching" 148.%V Vol. 3 149.%P pp. 170-178 150.Re 151.Rs 152.%A Paige, R. 153.%D 1987 154.%T "Three Partition Refinement Algorithms" 155.%J "SIAM J. Comput." 156.%V Vol. 16 157.%N No. 6 158.Re 159.Rs 160.%A McIlroy, P. 161.%D 1993 162.%B "Engineering Radix Sort" 163.%T "Computing Systems" 164.%V Vol. 6:1 165.%P pp. 5-27 166.Re 167.Sh HISTORY 168The 169.Fn radixsort 170function first appeared in 171.Bx 4.4 . 172