Radix Sort — JavaScript implementation

Radix sort does not compare numbers, instead, it takes advantage of number’s property, e.g. length, to fast sort numbers without having to do comparison.

  • Given a list of numbers
  • Find the maximum length among all numbers, that is, the largest number’s length
  • Start a loop from 0 up to the maximum length
    - i means the i-th position counting from the right
    - In each iteration, we sort the numbers by the digit at the specified i position.

For example, we want to sort this array of numbers:

The largest number = 9763, the maximum length = 4, so we will have 4 iterations.

Iteration 0: Sort the array by the digit at index 0,
Note that index is counting from right

Iteration 1: Sort the array by the digit at index 1

Iteration 2: Sort the array by the digit at index 2

Iteration 3: Sort the array by the digit at index 3

Implementation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store