You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In computer science, radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. Radix sort dates back as far as 1887 to the work of Herman Hollerith on tabulating machines.
3
+
In computer science, **radix sort** is a non-comparative integer sorting
4
+
algorithm that sorts data with integer keys by grouping keys by the individual
5
+
digits which share the same significant position and value. A positional notation
6
+
is required, but because integers can represent strings of characters
7
+
(e.g., names or dates) and specially formatted floating point numbers, radix
8
+
sort is not limited to integers.
4
9
5
10
## Efficiency
6
11
7
-
The topic of the efficiency of radix sort compared to other sorting algorithms is somewhat tricky and subject to quite a lot of misunderstandings. Whether radix sort is equally efficient, less efficient or more efficient than the best comparison-based algorithms depends on the details of the assumptions made. Radix sort complexity is O(wn) for n keys which are integers of word size w. Sometimes w is presented as a constant, which would make radix sort better (for sufficiently large n) than the best comparison-based sorting algorithms, which all perform O(n log n) comparisons to sort n keys. However, in general w cannot be considered a constant: if all n keys are distinct, then w has to be at least log n for a random-access machine to be able to store them in memory, which gives at best a time complexity O(n log n).[2] That would seem to make radix sort at most equally efficient as the best comparison-based sorts (and worse if keys are much longer than log n).
8
-
9
-
The counter argument is that comparison-based algorithms are measured in number of comparisons, not actual time complexity. Under some assumptions the comparisons will be constant time on average, under others they will not. Comparisons of randomly generated keys takes constant time on average, as keys differ on the very first bit in half the cases, and differ on the second bit in half of the remaining half, and so on, resulting in an average of two bits that need to be compared. In a sorting algorithm the first comparisons made satisfies the randomness condition, but as the sort progresses the keys compared are clearly not randomly chosen anymore. For example, consider a bottom-up merge sort. The first pass will compare pairs of random keys, but the last pass will compare keys that are very close in the sorting order. This makes merge sort, on this class of inputs, take O(n (log n)2) time. That assumes all memory accesses cost the same, which is not a physically reasonable assumption as we scale n to infinity, and not, in practice, how real computers work.
12
+
The topic of the efficiency of radix sort compared to other sorting algorithms is
13
+
somewhat tricky and subject to quite a lot of misunderstandings. Whether radix
14
+
sort is equally efficient, less efficient or more efficient than the best
15
+
comparison-based algorithms depends on the details of the assumptions made.
16
+
Radix sort complexity is `O(wn)` for `n` keys which are integers of word size `w`.
17
+
Sometimes `w` is presented as a constant, which would make radix sort better
18
+
(for sufficiently large `n`) than the best comparison-based sorting algorithms,
19
+
which all perform `O(n log n)` comparisons to sort `n` keys. However, in
20
+
general `w` cannot be considered a constant: if all `n` keys are distinct,
21
+
then `w` has to be at least `log n` for a random-access machine to be able to
22
+
store them in memory, which gives at best a time complexity `O(n log n)`. That
23
+
would seem to make radix sort at most equally efficient as the best
24
+
comparison-based sorts (and worse if keys are much longer than `log n`).
0 commit comments