SOURCE

const utils = {
  swap (array, a, b) {
    [array[a], array[b]] = [array[b], array[a]]
  },
  randomNum () {
    return Math.floor(Math.random() * 100)
  },
  randomArray () {
    return Array.from(Array(this.randomNum()), _ => this.randomNum() * 50 + this.randomNum())
  }
}

function radixSort (array) {
  let maxLength = 0
  for (let v of array) {
    let length = String(v).length
    if (length > maxLength) {
      maxLength = length
    }
  }
  for (i = 0; i < maxLength; i++) {
    array = sort(array, i)
  }

  function sort (array, index) {
      console.log(array);
    let buckets = []
    for (let i = 0; i < 10; i++) {
      buckets.push([])
    }
    for (let v of array) {
      let pad = String(v).padStart(maxLength, '0')
      let num = pad[maxLength - 1 - index]
      buckets[num].push(v)
    }
    console.log(buckets);
    let result = []
    for (let bucket of buckets) {
      result.push(...bucket)
    }
    console.log(result);
    return result
  }
  return array
}

let array = radixSort([3, 8, 6, 1, 5, 7, 9, 2, 4])
console.log(array)
console 命令行工具 X clear

                    
>
console