Sorting

Radix Sort

Size:15
Execution Narrative
Step 1 of 67Start
Line 1
Starting Radix Sort (LSD) on array of 15 elements.
Comparisons0
Swaps0
Operations0

Overview & Concept

Radix Sort is a non-comparative integer sorting algorithm that sorts data by processing individual digits, either from Least Significant Digit (LSD) to Most Significant Digit (MSD). It uses a stable sub-sorting algorithm (typically Counting Sort) for each digit place value, achieving linear O(d × (n + b)) time.

How It Works

Digit Extraction: Numbers are evaluated digit-by-digit from units, tens, hundreds, up to max digits d.

Stable Bucket Sorting: A stable sorting pass (Counting Sort with base b = 10) sorts items by the current digit place.

Preserved Order: Because each pass is stable, earlier sorted lower digits remain in correct relative order.

Step-by-Step Execution

1. Find Maximum ValueDetermine the maximum number to calculate total digits d.
2. Initialize Place ValueSet place = 1 (units digit).
3. Counting Sort on DigitSort array based on (A[i] / place) % 10 using stable counting sort.
4. Advance Digit PlaceSet place = place * 10; repeat until all d digits are processed.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(d · (n + k))O(d · (n + k))O(d · (n + k))O(n + k)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
procedure radixSort(A):
  find maximum value in A
  exp = 1 // start with least significant digit
  while max / exp > 0:
    distribute elements into 10 buckets by digit (A[i]/exp)%10
    collect elements back from buckets in stable order
    exp *= 10 // move to next digit place
  return A
end while
return A
Algorithmic Invariants
  • Digit Subarray Sortedness: After pass p, the array is strictly sorted according to the lower p digits.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Linear O(d × n) time complexity: significantly outperforms comparison sorts when digit count d is small.
  • •Stable sorting.

Trade-offs & Limitations

  • •Requires O(n + b) auxiliary memory for output and bucket arrays.
  • •Performance degrades if keys are extremely wide with large d.

Real-World Applications

Fixed-Length Key Sorting

Sorting phone numbers, postal codes, and IP addresses.

Suffix Array Construction

Fast suffix sorting in bioinformatics and text search engines.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When sorting large volumes of fixed-width integers or strings.

Consider Alternatives When

  • ✕When key lengths vary wildly or comparison-based sorts are sufficient.

Compare trade-offs and complexity across complementary tools in AlgoViz.