Imagine sorting a pile of coins. Instead of comparing values directly, Radix Sort separates them based on the value of their least significant digit (like separating pennies from nickels). Then, it refines further by the next digit, like sorting nickels by their second digit. This intuitive, digit-by-digit approach leads to a beautifully sorted pile.
Process
Radix Sort, as the name suggests, takes inspiration from how we read numbers: digit by digit. Instead of complex comparisons, it sorts data by analyzing individual digits, making it particularly efficient for numbers or strings with fixed lengths. Let’s embark on this digit-by-digit sorting journey:
1. Digit Dive: Imagine a bucket brigade, where each bucket represents a possible value for a specific digit (0-9 in most cases). Radix Sort starts by analyzing the least significant digit (LSD) of each element. It then iterates through the data, throwing each element into the bucket corresponding to its LSD value.
2. Bucket Brigade: Now, each bucket holds elements with the same LSD. Radix Sort processes each bucket independently, potentially using another sorting technique like counting sort within each bucket. This ensures all elements within a bucket share the same LSD and are partially sorted.
3. Refining Order: Once all buckets are processed, it’s time for the next digit! Radix Sort shifts its focus to the second least significant digit (SLSD) and repeats the bucket brigade process. Now, elements are further sorted based on their SLSD within each bucket, refining the overall order.
4. Digit March: This digit-by-digit sorting continues, working its way up to the most significant digit (MSD). With each pass, the buckets contain increasingly refined groups of elements, and the overall order becomes clearer.
5. Final Assembly: After processing the MSD, all elements are finally in their correct positions within the main array. Radix Sort gathers them back from the buckets, resulting in a beautifully sorted list!
Pseudo Code
Code
function radixSort(array, d):
# d is the maximum number of digits (adjust based on your data)
# Create an empty array of buckets (0-9 in each bucket)
buckets = [[None] * 10 for _ in range(d)]
# Perform passes for each digit from LSD to MSD
for digit in range(d):
# Distribute elements into buckets based on their digit at the current position
for num in array:
digitValue = (num // 10**digit) % 10 # Extract the current digit
buckets[digitValue].append(num)
# Gather elements back from buckets in order
arrayIndex = 0
for bucket in buckets:
for num in bucket:
array[arrayIndex] = num
arrayIndex += 1
# Clear the bucket for the next iteration
bucket[:] = []
# Return the sorted array
return array
Explanation
radixSort
: This function takes the array to be sorted and the maximum number of digits (d
) as input.buckets
: This array stores 10 sub-arrays, representing buckets for each possible digit (0-9).digit
loop: This loop iterates through each digit position, from least significant (LSD) to most significant (MSD).- Distributing elements: For each element in the array, its digit at the current
digit
position is extracted, and the element is placed in the corresponding bucket. - Gathering elements: After processing all elements for the current digit, the function iterates through each bucket and gathers the elements back into the main array in the order they appear in the buckets.
- Clearing buckets: The buckets are cleared after each iteration to prepare for the next digit.
- Return: The sorted array is returned.