0% completed
Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. It sorts numbers starting from the least significant digit (LSD) to the most significant digit (MSD). This method ensures that numbers are sorted correctly at each digit level before moving to the next. Radix Sort uses a stable sorting algorithm, typically Counting Sort, as a subroutine to sort digits.
The key idea is to sort the numbers digit by digit, ensuring that after each pass, the numbers are partially sorted based on the current digit. This process is repeated for each digit position, resulting in a fully sorted array. Radix Sort is particularly effective for sorting large numbers or fixed-length strings, where the number of digits (or characters) is constant.
Find the Maximum Value in the Array
Traverse the array to find the maximum value.
Process Each Digit Starting from the Rightmost
exp
to 1
.exp
by multiplying it by 10 to move to the next digit to the left (tens place, hundreds place, etc.).Counting Sort Based on Digit Position
Input: array1 = [180, 55, 85, 90, 903, 243, 2, 6]
Find the Maximum Value
Digit Position: Units (exp = 1)
Count Digit Occurrences:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
180
: digit = 0, count array becomes [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
55
: digit = 5, count array becomes [1, 0, 0, 0, 0, 1, 0, 0, 0, 0]
85
: digit = 5, count array becomes [1, 0, 0, 0, 0, 2, 0, 0, 0, 0]
90
: digit = 0, count array becomes [2, 0, 0, 0, 0, 2, 0, 0, 0, 0]
903
: digit = 3, count array becomes [2, 0, 0, 1, 0, 2, 0, 0, 0, 0]
243
: digit = 3, count array becomes [2, 0, 0, 2, 0, 2, 0, 0, 0, 0]
2
: digit = 2, count array becomes [2, 0, 1, 2, 0, 2, 0, 0, 0, 0]
6
: digit = 6, count array becomes [2, 0, 1, 2, 0, 2, 1, 0, 0, 0]
Cumulative Count:
[2, 2, 3, 5, 5, 7, 8, 8, 8, 8]
Build Output Array:
6
: digit = 6, place 6
at index count[6]- 1 = 8 - 1 = 7
.
[2, 2, 3, 5, 5, 7, 7, 8, 8, 8]
.[0, 0, 0, 0, 0, 0, 0, 6]
.2
: digit = 2, place 2
at index count[2] - 1 = 3 - 1 = 2
.
[2, 2, 2, 5, 5, 7, 7, 8, 8, 8]
.[0, 0, 2, 0, 0, 0, 0, 6]
.243
: digit = 3, place 243
at index 4
.
[2, 2, 2, 4, 5, 7, 7, 8, 8, 8]
.[0, 0, 2, 0, 243, 0, 0, 6]
.903
: digit = 3, place 903
at index 3
.
[2, 2, 2, 3, 5, 7, 7, 8, 8, 8]
.[0, 0, 2, 903, 243, 0, 0, 6]
.90
: digit = 0, place 90
at index 1
.
[1, 2, 2, 3, 5, 7, 7, 8, 8, 8]
.[0, 90, 2, 903, 243, 0, 0, 6]
.85
: digit = 5, place 85
at index 6
.
[1, 2, 2, 3, 5, 6, 7, 8, 8, 8]
.[0, 90, 2, 903, 243, 0, 85, 6]
.55
: digit = 5, place 55
at index 5
[1, 2, 2, 3, 5, 5, 7, 8, 8, 8]
.[0, 90, 2, 903, 243, 55, 85, 6]
.180
: digit = 0, place 180
at index 0
.
[0, 2, 2, 3, 5, 5, 7, 8, 8, 8]
.[180, 90, 2, 903, 243, 55, 85, 6]
.Copy to Original Array:
array
becomes [180, 90, 2, 903, 243, 55, 85, 6]
Digit Position: Tens (exp = 10)
Count Digit Occurrences:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Cumulative Count:
[3, 3, 3, 3, 4, 5, 5, 5, 7, 8]
Build Output Array:
6
: digit = 0, place 6
at index 2
.
[2, 3, 3, 3, 4, 5, 5, 5, 7, 8]
.[0, 0, 6, 0, 0, 0, 0, 0]
85
: digit = 8, place 85
at index 6
.
[2, 3, 3, 3, 4, 5, 5, 5, 6, 8]
.[0, 0, 6, 0, 0, 0, 85, 0]
55
: digit = 5, place 55
at index 4
.
[2, 3, 3, 3, 4, 4, 5, 5, 6, 8]
.[0, 0, 6, 0, 55, 0, 85, 0]
243
: digit = 4, place 243
at index 3
.
[2, 3, 3, 3, 3, 4, 5, 5, 6, 8]
.[0, 0, 6, 243, 55, 0, 85, 0]
903
: digit = 0, place 903
at index 1
.
[1, 3, 3, 3, 3, 4, 5, 5, 6, 8]
.[0, 903, 6, 243, 55, 0, 85, 0]
2
: digit = 0, place 2
at index 0
.
[0, 3, 3, 3, 3, 4, 5, 5, 6, 8]
.[2, 903, 6, 243, 55, 0, 85, 0]
90
: digit = 9, place 90
at index 7
.
[0, 3, 3, 3, 3, 4, 5, 5, 6, 7]
.[2, 903, 6, 243, 55, 0, 85, 90]
180
: digit = 8, place 180
at index 5
.
[0, 3, 3, 3, 3, 4, 5, 5, 5, 7]
.[2, 903, 6, 243, 55, 180, 85, 90]
Copy to Original Array:
array
becomes [2, 903, 6, 243, 55, 180, 85, 90]
Digit Position: Hundreds (exp = 100)
Count Digit Occurrences:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 1, 1, 0, 0, 0, 0, 0, 0, 1]
Cumulative Count:
[5, 6, 7, 7, 7, 7, 7, 7, 7, 8]
Build Output Array:
90
: digit = 0, place 90
at index 4
.
[4, 6, 7, 7, 7, 7, 7, 7, 7, 8]
.[0, 0, 0, 0, 90, 0, 0, 0]
85
: digit = 0, place 85
at index 3
.
[3, 6, 7, 7, 7, 7, 7, 7, 7, 8]
.[0, 0, 0, 85, 90, 0, 0, 0]
180
: digit = 1, place 180
at index 5
.
[3, 5, 7, 7, 7, 7, 7, 7, 7, 8]
.[0, 0, 0, 85, 90, 180, 0, 0]
55
: digit = 0, place 55
at index 2
.
[2, 5, 7, 7, 7, 7, 7, 7, 7, 8]
.[0, 0, 55, 85, 90, 180, 0, 0]
243
: digit = 2, place 243
at index 6
.
[2, 5, 6, 7, 7, 7, 7, 7, 7, 8]
.[0, 0, 55, 85, 90, 180, 243, 0]
6
: digit = 0, place 6
at index 1
.
[1, 5, 6, 7, 7, 7, 7, 7, 7, 8]
.[0, 6, 55, 85, 90, 180, 243, 0]
903
: digit = 9, place 903
at index 7
.
[1, 5, 6, 7, 7, 7, 7, 7, 7, 7]
.[0, 6, 55, 85, 90, 180, 243, 903]
2
: digit = 0, place 2
at index 0
.
[0, 5, 6, 7, 7, 7, 7, 7, 7, 7]
.[2, 6, 55, 85, 90, 180, 243, 903]
Copy to Original Array:
array
becomes [2, 6, 55, 85, 90, 180, 243, 903]
Overall time complexity: O(d * (n + k)), where d is the number of digits, n is the number of elements, and k is the range of the digits.
Overall space complexity: $O(n + k)%
.....
.....
.....