3 ms·
This library is essentially a generic version of radix sort. For example to sort an array of strings in O(nk), where k is the maximum length all strings: use
by twanvl 8y ago
This library is essentially a generic version of radix sort. For example to sort an array of strings in O(nk), where k is the maximum length all strings:
use bucket sort to split the strings into 256 buckets based on the first character. Take O(n)
* recursively sort each bucket based on the remaining k-1 characters. Takes O(n(k-1)).
concatenate sorted buckets
This is better than the O(nklog(n)) you get with comparison based sort. It turns out that you can implement radix sort for pretty much any type, where instead of a comparison operator for that type you have a 'split into buckets' operator.
The types might look something like this (in Haskell notation):
class Ord a where
compare :: a -> a -> Ordering
class Bucketable a where
splitIntoBuckets :: (b -> a) -> [b] -> [[b]]