Radix Sort

Thinking:

let arr = [505, 8, 65, 931]

const radixSort = (arr) => {
  const max = Math.max(...arr)
  // define a two-dimensional bucket array
  let buckets = Array.from({length: 10}, () => [])
  console.log(buckets)
  let mark = 1
  // when mark < max. we need to do m*=10,
  // this is the way to become tens digits, 100s digits or larger
  // it helps to make sure we traverse all digits
  while (mark < max) {
    // get number for each digit(10s digit, 100s digit, 1000s digits)
    arr.forEach(num => {
      //取余的的概念不牢固:65%100 余数是65
      const c = ((num % (mark * 10)) / mark)
      console.log(Number.isInteger(c))
      console.log(c)
      const digitValue = Math.floor((num % (mark * 10)) / mark)
      buckets[digitValue].push(num)
    })
    let i = 0
    buckets.forEach(bucket => {
      while (bucket.length > 0) {
        arr[i++] = bucket.shift()
      }
    })
    // 每次最外层while循环后m要乘以10
    // 也就是要判断下一位,比如当前是个位,下次就要判断十位
    mark *= 10
  }
  return arr
}

console.log(radixSort(arr))

Domain Name