3 Radix-Sort
3 Radix-Sort
Characteristics
Non-Comparative: Unlike traditional sorting algorithms (e.g.,
quicksort, mergesort) that compare elements directly, Radix sort
organizes elements based on their digit values.
Stable: Radix sort preserves the order of equal elements,
making it suitable for applications where the order of similar
elements is significant.
Linear Time Complexity: The average and worst-case time
complexity can be O(d * (n + k)), where:
o d is the number of digits in the largest number.
o n is the number of elements in the array.
o k is the range of the input (for decimal digits, this would
be 0-9).
Example
Input: [170, 45, 75, 90, 802, 24, 2, 66]
Pass 1 (Least Significant Digit): Sort based on the unit place
→ [170, 90, 802, 2, 24, 45, 75, 66]
Pass 2: Sort based on the tens place → [170, 802, 2, 24, 45, 75,
90, 66]
Pass 3 (Most Significant Digit): Sort based on the hundreds
place → [2, 24, 45, 66, 75, 90, 170, 802]
Advantages
Efficiency: Radix sort can be faster than comparison-based sorts
(like quicksort or mergesort) for integers with a limited range of
digits.
Non-Comparison Based: It doesn't compare elements directly,
which can lead to better performance in specific scenarios.
Disadvantages
Space Complexity: Requires additional space for counting and
temporary arrays, making it less memory-efficient than in-place
algorithms.
Limited to Integers: Primarily used for integers or strings; not
applicable for all data types without modification.
Dependency on Digit Range: Performance can degrade if the
range of digits (or characters) is large.