Radix Sort
📚 Radix Sort
-
Radix Sort is a non-comparison based integer sorting algorithm.
-
It sorts numbers digit by digit, starting from the least significant digit (LSD) to the most significant digit (MSD).
-
It uses a stable sub-sorting algorithm like Counting Sort at each digit position.
👉 Idea:
✅ Key Point: Radix sort is very fast when number of digits is small compared to number of elements.
🛠️ Algorithm for Radix Sort
-
Find the maximum number to know the number of digits.
-
Start from Least Significant Digit (LSD).
-
Use stable counting sort based on the current digit.
-
Repeat for every digit (unit place, ten place, hundred place, etc).
🔥 Example
Let's sort:
[170, 45, 75, 90, 802, 24, 2, 66]
Step 1: Sort according to 1s place
-
170 → 0
-
45 → 5
-
75 → 5
-
90 → 0
-
802 → 2
-
24 → 4
-
2 → 2
-
66 → 6
Order after 1s place sort:
[170, 90, 802, 2, 24, 45, 75, 66]
Step 2: Sort according to 10s place
-
170 → 7
-
90 → 9
-
802 → 0
-
2 → 0
-
24 → 2
-
45 → 4
-
75 → 7
-
66 → 6
Order after 10s place sort:
[802, 2, 24, 45, 66, 170, 75, 90]
Step 3: Sort according to 100s place
-
802 → 8
-
2 → 0
-
24 → 0
-
45 → 0
-
66 → 0
-
170 → 1
-
75 → 0
-
90 → 0
Order after 100s place sort:
[2, 24, 45, 66, 75, 90, 170, 802]
✅ Now the array is sorted!
📋 Algorithm
📈 Complexity
Feature | Details |
---|---|
Time Complexity | O(d*(n + k)) where d = number of digits, n = number of elements, k = base (usually 10) |
Space Complexity | O(n + k) |
Stable Sorting | ✅ Yes |
Best for | Sorting integers, large number of elements with small digit size |
📌 Important Points:
-
Radix Sort does not compare elements.
-
It is stable.
-
Works best when range of numbers is not huge.
-
For larger digits or variable-length strings, Radix Sort with MSD is used.
C Program - Radix Sort
6
Enter the array elements
170 45 75 90 802 24
Original array: 170 45 75 90 802 24
Sorted array: 24 45 75 90 170 802
Comments
Post a Comment