Radix sort is a sorting algorithm that operates on integers by examining individual digits. It is also known as bucket sort or digital sort.

The term “radix” refers to the base of a number system. In radix sort, numbers are sorted digit by digit from the least significant digit (rightmost) to the most significant digit (leftmost). This algorithm is particularly useful when dealing with large numbers or a large number of elements.

## How does Radix Sort work?

The radix sort algorithm works by distributing the elements into different buckets based on their digits. Each bucket represents a range of values for a specific digit.

After distributing the elements, the algorithm collects them back into a single list in the order of their buckets. This process is repeated for each subsequent digit until all digits have been processed.

### Example:

Let’s consider an array with unsorted integers: [170, 45, 75, 90, 802, 24, 2, 66]

#### First pass – Sorting by least significant digit (units place):

- 170
- 90
- 802
- 2
- 24
- 45
- 75
- 66

#### Buckets:

__Bucket for ‘0’:__**[170,90]****(empty)****(empty)****(empty)****(empty)****(empty)****(empty)****(empty)****(empty)**__Bucket for ‘2’:__**[2]****(empty)****(empty)****(empty)****(empty)**__Bucket for ‘4’:__

*Empty*__Bucket for ‘5’:__

*[45,75]*__Bucket for ‘6’:__

*[66]*__Bucket for ‘7’:__[802]__Bucket for ‘8’:__[802] (Subsequent passes will overwrite previous values) [170] (Subsequent passes will overwrite previous values) [90] (Subsequent passes will overwrite previous values) [24] (Subsequent passes will overwrite previous values) (Empty) (Empty) (Empty) (Empty) (Empty) (Empty) .. and so on#### List After Pass:

- [170,90,802,2,24,45,75,66]

#### Second pass – Sorting by tens place:

The algorithm distributes the elements into different buckets based on their tens place digit. After distributing the elements, the algorithm collects them back into a single list in the order of their buckets.

#### Third pass – Sorting by hundreds place:

The algorithm distributes the elements into different buckets based on their hundreds place digit.

## Complexity Analysis

The time complexity of radix sort depends on the number of digits in the maximum element of the input array. Let ‘d’ be the number of digits and ‘n’ be the number of elements in the array.

The time complexity is O(d * (n + b)), where ‘b’ is the base or range of possible values for each digit (usually 10 for decimal numbers). Radix sort has a linear time complexity when ‘d’ is relatively small compared to ‘n’. However, if ‘d’ is large, radix sort may not be as efficient as other sorting algorithms with better average case time complexities.

## Conclusion

Radix sort is an efficient sorting algorithm that operates on integers by examining individual digits. It sorts numbers digit by digit from least significant to most significant using bucket distribution and collection techniques.

Radix sort’s performance depends on the number of digits and range of possible values in the input array. It can be particularly useful when dealing with large numbers or a large number of elements.

If you are interested in learning more about sorting algorithms and data structures, make sure to check out our other tutorials!