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