DSA SheetLesson · no judge

SORTINGCOUNTING SORT / BUCKET SORT / RADIX SORT / CYCLIC SORT

Radix Sort

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 28

Radix sort can order whole numbers by looking at only one digit per pass

Radix sort does not compare the whole values in [170, 45, 75, 90, 802, 24, 2, 66]. Instead, it sorts the array repeatedly by one place value, starting with the least significant digit. The ones pass uses exp = 1, the tens pass uses exp = 10, and the hundreds pass uses exp = 100.

CPPExtract the digit at the place represented by exp.
int digit = (value / exp) % 10;

Integer division removes the digits to the right of the selected place, and % 10 keeps the digit that remains at the right. For example, when exp = 10, the expression extracts the tens digit. Each pass uses that digit as a bounded key from 0 through 9, so frequency counts and prefix positions can place the values efficiently.

The initial array is [170, 45, 75, 90, 802, 24, 2, 66]. During the ones pass, values are grouped by their ones digit. During the tens pass, they are grouped by their tens digit. During the hundreds pass, they are grouped by their hundreds digit. The word stably matters: values with the same selected digit keep the order they had before that pass.

CHECKPOINT 1Not answered

Which digit does (802 / 10) % 10 extract?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A stable pass preserves every useful decision made by earlier passes

The ones pass puts values into digit groups while preserving their original order inside each group. The values with ones digit 0 are 170 and 90, followed by 802 and 2 with ones digit 2, then 24, 45, 75, and 66 in their own digit groups. The result is [170, 90, 802, 2, 24, 45, 75, 66].

TEXTEqual current digits keep their earlier left-to-right order.
Original order: 45, 75
Ones digits:    5,  5
After ones pass: 45, 75

The tens pass now uses the result of the ones pass as its input. It produces [802, 2, 24, 45, 66, 170, 75, 90]. Notice that 170 comes before 75 even though both have tens digit 7. Their order is inherited from the earlier ones pass: 170 had ones digit 0 and 75 had ones digit 5. The tens pass groups them by 7 without reversing that useful lower-digit order.

This is why processing from least significant digit to most significant digit works. A later pass is allowed to rearrange different current-digit groups, but inside one equal-digit group it must retain the order already created by lower places. An unstable placement could put 75 before 170, losing the information that 170 is smaller at the ones place.

the array [170, 45, 75, 90, 802, 24, 2, 66] across its stable ones, tens, and hundreds passesFour horizontal rows trace the same eight values. The initial row is [170, 45, 75, 90, 802, 24, 2, 66]. The ones pass produces [170, 90, 802, 2, 24, 45, 75, 66]. The tens pass, treating missing tens digits as 0, produces [802, 2, 24, 45, 66, 170, 75, 90]. The hundreds pass, treating missing hundreds digits as 0, produces [2, 24, 45, 66, 75, 90, 170, 802]. Markers show that values with equal current digits keep their previous left-to-right order.170[0]45[5]75[5]90[0]802[2]24[4]2[2]66[6]170[0]90[0]802[2]2[2]24[4]45[5]75[5]66[6]802[0]2[0]24[2]45[4]66[6]170[7]75[7]90[9]2[0]24[0]45[0]66[0]75[0]90[0]170[1]802[8]stable LSD radix passesinitialonesafter onesones groups0 2 4 5 6after tenstens digitafter hundredshundreds digitstable order is preserved within each digit group
Stable digit passes turn the original order into complete numeric order.

Missing leading digits act as zeros, and the largest value determines the last pass

A value does not need to have as many digits as the maximum value. Treat every missing leading digit as 0 for the current place. Therefore, 2 has tens digit 0 and hundreds digit 0. In the tens pass, 802, 2, 24, 45, and 66 all begin the output according to their tens digits, with 802, 2, and 24 receiving tens digit 0 while stable order keeps 802 before 2 before 24.

EXPPLACE CHECKEDARRAY AFTER THE STABLE PASS
1ones[170, 90, 802, 2, 24, 45, 75, 66]
10tens[802, 2, 24, 45, 66, 170, 75, 90]
100hundreds[2, 24, 45, 66, 75, 90, 170, 802]
The pass order for the running array

The maximum value is 802, which has three digits. That means the required exp values are 1, 10, and 100. After the hundreds pass, every value has been ordered by all three relevant places. A pass with exp = 1000 would inspect a place that is 0 for every value, so it cannot change the ordering and is not needed.

CHECKPOINT 2Not answered

How many digit passes are needed to sort [170, 45, 75, 90, 802, 24, 2, 66]?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

After the final stable digit pass, the whole array is sorted

After the ones pass, the array is ordered by its last digit. After the tens pass, values with different tens digits are placed in the correct tens groups, while equal tens digits retain their ones-digit order. After the hundreds pass, the same rule adds the hundreds digit without destroying the order of the lower two places. The final result is [2, 24, 45, 66, 75, 90, 170, 802].

For base 10, each pass performs counting work over n values and 10 possible digit keys, then writes the n values into stable positions. If d is the number of digits in the maximum value, the total time is O(d * (n + 10)). The extra arrays for counts and stable output use O(n + 10) space.

The direct version described here assumes non-negative integers. That assumption makes every extracted digit a key from 0 through 9 and lets the maximum value determine how many passes are required. The algorithm's correctness comes from two facts together: passes run from least significant to most significant, and every pass is stable.

Previous · Counting SortNext part · Quiz