RADIXSORT(3)
π RADIXSORT(3) β the radix sort situation
okay so bestie, buckle up, we're talking about radixsort and sradixsort from the Library Functions Manual, section 3, living in the Standard C Library (libc, link it with -lc). this is literally the radix sort page and it's a whole vibe π
β¨ SYNOPSIS β¨
like, here's the whole ritual, copy it exactly or it will not slay:
#include <limits.h>
#include <stdlib.h>
int
radixsort(const unsigned char **base, int nmemb,
const unsigned char *table, unsigned endbyte);
int
sradixsort(const unsigned char **base, int nmemb,
const unsigned char *table, unsigned endbyte);
π¦ DESCRIPTION π¦
so radixsort() and sradixsort() are both implementations of radix sort. iconic behavior, no cap.
what they do: they sort an array of pointers to byte strings, and the very first member of that array is the thing referenced by base. the byte strings can literally contain any values you want π«Ά β the end of each string is marked by the value you specify, called endbyte.
πΈ the table argument (the fun part)
you can absolutely specify your own sort order by passing the table argument. here's the tea:
- if
tableis non-NULL, it must reference an array ofUCHAR_MAX + 1bytes, and that array contains the sort weight of every possible byte value. - the end-of-string byte must have a sort weight of
0or255(that255is for sorting in reverse order, yes ma'am). - more than one byte may have the same sort weight β that's not a bug, that's a feature.
tableis lowkey perfect for apps that want to sort different characters equally. for example: giveA-Zthe same weights asa-zand boom, you got yourself a case-insensitive sort β¨- if
tableisNULL, the array gets sorted in ascending order according to the ASCII order of the byte strings they reference, andendbytegets a sorting weight of0.
π stability check
sradixsort()is stable β meaning if two elements compare as equal, their order in the sorted array is unchanged. she does not shuffle your equals. but she does use additional memory, exactly enough to holdnmembpointers.radixsort()is not stable, but uses no additional memory. trade-offs, bestie π
π the algorithm lore
these functions are variants of most-significant-byte radix sorting. specifically, go look at D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. and the way they take linear time relative to the number of bytes in the strings?? obsessed. β
π₯ RETURN VALUES π₯
radixsort() returns the value 0 if successful. otherwise it returns the value -1 and the global variable errno is set to indicate the error. that's it, that's the whole deal.
π ERRORS π
[EINVAL]β the value of theendbyteelement oftableis not0or255. (you had ONE job π€)
plus, sradixsort() may also fail and set errno for any of the errors specified for the library routine malloc(3). so like, memory things. keep an eye on it.
π SEE ALSO π
sort(1),qsort(3)- Knuth, D.E., βSorting and Searchingβ, The Art of Computer Programming, Vol. 3, pp. 170-178, 1968.
- Paige, R., βThree Partition Refinement Algorithmsβ, SIAM J. Comput., No. 6, Vol. 16, 1987.
- McIlroy, P., βComputing Systemsβ, Engineering Radix Sort, Vol. 6:1, pp. 5-27, 1993.
π§ HISTORY π§
the radixsort() function first appeared in 4.4BSD. she's been serving since the old days π
and that's the page, bestie β this one shipped on Debian, dated January 27, 1994. ancient icon behavior fr fr, go sort something ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms