# What Is Radix Sort in Data Structure With Example?

//

Larry Thompson

What Is Radix Sort in Data Structure With Example?

In the world of sorting algorithms, radix sort is a unique and efficient method used to sort integers or strings based on their individual digits or characters. It belongs to the category of non-comparison-based sorting algorithms, meaning it does not compare elements directly like other popular sorting algorithms such as bubble sort or merge sort.

## How Does Radix Sort Work?

The underlying principle behind radix sort is to sort elements digit by digit or character by character from the least significant position to the most significant position. This means that instead of comparing elements directly, radix sort groups them based on their individual digits or characters.

Let’s understand this concept with an example:

Example:

• Consider an array of integers: [170, 45, 75, 90, 802, 24, 2, 66]
• In the first iteration of radix sort, we group the numbers based on their rightmost digit (i.e., ones place). The resulting groups are: [802, 2], [24], [45], [75], [90], [170], [66].
• In the second iteration, we group the numbers based on their tens place.

The resulting groups are: [802, 2], [24], [45], [66], [75], [90], [170].

• In the third and final iteration for this example (as there are no more digits), we group the numbers based on their hundreds place. The resulting groups are: [2, 24, 45, 66, 75, 90, 170].

• Efficiency: Radix sort can be faster than other sorting algorithms, especially when dealing with a large number of elements or long strings.
• Stability: Unlike some other sorting algorithms, radix sort is stable, meaning it preserves the relative order of elements with equal values. This property is useful when sorting objects with multiple attributes.