RadixSort is a non-comparison based sorting algorithm that sorts integers by processing individual digits. It processes the digits from least significant to most significant (LSD Radix Sort) or from most significant to least significant (MSD Radix Sort).

This implementation uses LSD (Least Significant Digit) Radix Sort, which processes digits from right to left. The algorithm uses counting sort as a subroutine to sort the array based on each digit.

Radix sort is particularly efficient for sorting integers with a fixed number of digits. The time complexity is O(d * (n + k)) where n is the number of elements, d is the number of digits in the maximum number, and k is the range of each digit (typically 10 for decimal). The space complexity is O(n + k).

Note: This implementation works with non-negative integers. For negative numbers, they are handled by separating them, sorting, and recombining.

Example

const radixSort = new RadixSort([170, 45, 75, 90, 802, 24, 2, 66]);
radixSort.sort(); // returns new array with [2, 24, 45, 66, 75, 90, 170, 802]
// or
RadixSort.sort([170, 45, 75, 90, 802, 24, 2, 66]); // returns new array with [2, 24, 45, 66, 75, 90, 170, 802]

Example

const radixSort = new RadixSort([5, 3, 8, 6, 2, 7, 1, 4]);
radixSort.sort(); // returns new array with [1, 2, 3, 4, 5, 6, 7, 8]

Hierarchy

  • RadixSort

Constructors

Properties

size: number
values: number[]

Methods

  • Sorts the array using the RadixSort algorithm.

    Returns number[]

    a new array with the elements sorted

    Example

    const radixSort = new RadixSort([170, 45, 75, 90, 802, 24, 2, 66]);
    radixSort.sort(); // returns new array with [2, 24, 45, 66, 75, 90, 170, 802]
  • Perform counting sort on an array based on a specific digit position

    Parameters

    • array: number[]

      the array to sort

    • digitPos: number

      the digit position to sort by

    Returns number[]

    the sorted array for this digit position

  • Count the number of digits in a number

    Parameters

    • num: number

      the number to count digits of

    Returns number

    the number of digits

  • Get the digit at a specific position (from right to left, 0-indexed)

    Parameters

    • num: number

      the number to get the digit from

    • digitPos: number

      the position of the digit (0 = rightmost)

    Returns number

    the digit at the specified position

  • Find the maximum number of digits in an array

    Parameters

    • array: number[]

      the array to find the max digits in

    Returns number

    the maximum number of digits

  • Sorts the array using the RadixSort algorithm. Static method, no need to instantiate class

    Parameters

    • array: number[]

      the array of integers to be sorted

    Returns number[]

    a new array with the elements sorted

Generated using TypeDoc