THINK FIRST·CODE LATER

← All labs

LSD radix sort, pass by pass

Problem

Write RadixSort for non-negative integers. Sort by the least significant digit first: one stable counting-sort pass per decimal digit (units, tens, hundreds, …), as many passes as the largest value has digits. Print the array after every pass.

For the input

8
170 45 75 90 802 24 2 66

the program prints

after digit 1: [170, 90, 802, 2, 24, 45, 75, 66]
after digit 10: [802, 2, 24, 45, 66, 170, 75, 90]
after digit 100: [2, 24, 45, 66, 75, 90, 170, 802]
Sorted in 3 passes (the largest value has 3 digits)

Why would the result be wrong if a pass were not stable?

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d9-5-data-1.txt

8
170 45 75 90 802 24 2 66
What your program must print for d9-5-data-1.txt
after digit 1: [170, 90, 802, 2, 24, 45, 75, 66]
after digit 10: [802, 2, 24, 45, 66, 170, 75, 90]
after digit 100: [2, 24, 45, 66, 75, 90, 170, 802]
Sorted in 3 passes (the largest value has 3 digits)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d9-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content d9-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.