All Exams Test series for 1 year @ ₹349 only
Question

Sort the following list using the Radix sort algorithm.

329, 457, 839, 436, 720, 355, 657

What is the output of the algorithm after the second pass?

The correct answer is

720, 329, 436, 839, 355, 457, 657

Understanding Radix Sort Algorithm

Radix sort is a non-comparative sorting algorithm. It sorts numbers by processing digits from least significant to most significant (LSD Radix Sort). It works by sorting the list based on the units digit, then the tens digit, then the hundreds digit, and so on. It requires a stable sorting algorithm (like Counting Sort) for each digit pass to maintain the relative order of elements with the same digit value.

Let's apply Radix sort to the given list: 329, 457, 839, 436, 720, 355, 657.

Pass 1: Sorting by Units Digit

We look at the units digit of each number and sort the list stably based on these digits. The units digits are: 9 (329), 7 (457), 9 (839), 6 (436), 0 (720), 5 (355), 7 (657).

We can imagine placing numbers into buckets based on their units digit:

  • Bucket 0: 720 (Units digit 0)
  • Bucket 1:
  • Bucket 2:
  • Bucket 3:
  • Bucket 4:
  • Bucket 5: 355 (Units digit 5)
  • Bucket 6: 436 (Units digit 6)
  • Bucket 7: 457, 657 (Units digit 7, maintaining original order of 457 before 657)
  • Bucket 8:
  • Bucket 9: 329, 839 (Units digit 9, maintaining original order of 329 before 839)

Concatenating the buckets from 0 to 9 gives the list after the first pass:

List after Pass 1: 720, 355, 436, 457, 657, 329, 839

Pass 2: Sorting by Tens Digit

Now, we take the list from Pass 1 (720, 355, 436, 457, 657, 329, 839) and sort it stably based on the tens digit. The tens digits are: 2 (720), 5 (355), 3 (436), 5 (457), 5 (657), 2 (329), 3 (839).

We place these numbers into buckets based on their tens digit, maintaining the order from the previous pass:

  • Bucket 0:
  • Bucket 1:
  • Bucket 2: 720, 329 (Tens digit 2, maintaining original order of 720 before 329)
  • Bucket 3: 436, 839 (Tens digit 3, maintaining original order of 436 before 839)
  • Bucket 4:
  • Bucket 5: 355, 457, 657 (Tens digit 5, maintaining original order: 355, then 457, then 657)
  • Bucket 6:
  • Bucket 7:
  • Bucket 8:
  • Bucket 9:

Concatenating the buckets from 0 to 9 gives the list after the second pass:

List after Pass 2: 720, 329, 436, 839, 355, 457, 657

Comparing with Options

Let's compare the resulting list after the second pass with the given options:

  • Option 1: 329, 355, 436, 457, 657, 720, 839
  • Option 2: 355, 329, 457, 436, 720, 657, 839
  • Option 3: 720, 329, 436, 839, 355, 457, 657
  • Option 4: 720, 355, 436, 457, 657, 329, 839

The list obtained after the second pass, 720, 329, 436, 839, 355, 457, 657, matches Option 3.

Original ListUnits Digit
3299
4577
8399
4366
7200
3555
6577
List after Pass 1Tens Digit
7202
3555
4363
4575
6575
3292
8393
List after Pass 2
720
329
436
839
355
457
657

Revision Table: Key Concepts in Radix Sort

ConceptDescriptionImportance
Least Significant Digit (LSD)Sorting begins with the rightmost digit (units place).Forms the basis of the common Radix sort implementation.
Most Significant Digit (MSD)Sorting can also begin with the leftmost digit, but is less common for integers.Different approach, often involves recursion.
Stable SortA sorting algorithm that maintains the relative order of equal elements.Crucial for Radix sort to work correctly across multiple passes. Counting sort is a common stable sort used per digit.
Number of PassesEquals the maximum number of digits in the largest number.Determines the complexity of the algorithm. For a list of $N$ numbers with maximum $D$ digits, time complexity is typically $\mathcal{O}(N \cdot D)$.
Digit BucketsTemporary storage (like arrays or lists) for numbers based on their current digit value.Used in the Counting sort step for each digit pass.

Additional Information: Radix Sort vs Other Algorithms

Radix sort is different from comparison-based sorting algorithms like Merge Sort, Quick Sort, or Heap Sort, which rely on comparing elements. Its time complexity is typically better for certain types of data, especially when the range of key values (digits in this case) is small and the number of items is large. For $N$ numbers with a maximum of $k$ digits, where each digit can take up to $r$ values, the time complexity is $\mathcal{O}(k(N+r))$. When $r$ is small and constant (like 10 for decimal digits) and $k$ is related to $\log N$ (e.g., numbers up to $N^c$), this can be faster than $\mathcal{O}(N \log N)$.

Key advantages of Radix sort:

  • Can be faster than comparison sorts for certain data distributions.
  • Useful for sorting fixed-length keys (like integers or strings of the same length).

Key disadvantages:

  • Only works for integers or data that can be represented as integers.
  • Requires extra space for buckets.
  • Can be slower than comparison sorts if the range of digits is very large or the number of digits is large relative to $N$.

The stability of the inner sorting algorithm (like Counting Sort) is essential. If an unstable sort were used for a digit pass, the correct relative order of numbers with the same digit in that pass would be lost, which would mess up subsequent passes and the final sorted result.

Was this answer helpful?

Important Questions from Sorting - Teaching

  1. What is the best case time complexity of radix sort algorithm?

  2. Merge sort uses:

  3. A sorting technique that guarantees that records with the same primary key occurs in the same

    order in the sorted list as in the original unsorted list is said to be
  4. Which of the following algorithm design approach is used in Quick sort algorithm?

  5. Which of the following is best running time to sort n integers in the range 0 to n 2-1

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App