Radix Sort:
Radix Sort:
What is Radix Sort?
Radix Sort is a way to arrange numbers in order by sorting them digit by digit, starting from the smallest digit (like ones, tens, hundreds) and working up.
How does it work?
- First, look at the ones place of all the numbers and sort them based on that.
- Then, look at the tens place and sort again.
- Next, look at the hundreds place, and so on.
- Repeat this for all the digits in the largest number. By the end, the list will be sorted.
Step-by-step process:
- Start with the smallest digit (ones).
- Group numbers based on that digit (like placing them into buckets for 0, 1, 2, etc.).
- Merge the buckets back into a single list.
- Move to the next digit (tens, hundreds) and repeat until all digits are sorted.
Example:
List: [170, 45, 75, 90, 802, 24, 2, 66]
Step 1: Look at the ones place:
Group: [170, 90] (0s), [802, 2] (2s), [24] (4s), [75, 45] (5s), [66] (6s)
Merge: [170, 90, 802, 2, 24, 75, 45, 66]Step 2: Look at the tens place:
Group: [802, 2] (0s), [170, 66] (6s), [24, 45] (4s), [75, 90] (9s)
Merge: [802, 2, 170, 66, 24, 45, 75, 90]Step 3: Look at the hundreds place:
Group: [2, 24, 45, 66, 75, 90] (0s), [170] (1s), [802] (8s)
Merge: [2, 24, 45, 66, 75, 90, 170, 802]
Now the list is sorted!
Key idea:
Sort the numbers digit by digit, starting from the smallest place value (ones) to the largest, until everything is in order.
Comments
Post a Comment