为什么我的基数排序实现不起作用 - JavaScript

Why my Implementation of Radix Sort doesn't work - JavaScript

这是我的代码:

// get digit at a specific location. 
// getDigit(12345,0) -> output: 5 
function getDigit(num1, i) {
    num1 = String(num1);
    num1 = num1.split('').reverse().join('');
    if (i>= num1.length) { return 0 }
    else { return parseInt(num1[i]) }
}


function mostDigits(array1) {
    if (array1.length == 0) {
        return 0
    }
    array1 = array1.map(x => String(x).length)
    return Math.max(...array1)
}
function radixSort(nums) {

    let array1 = [[], [], [], [], [], [], [], [], [], []]
    let maxnum = mostDigits(nums);
    for (let i = 0; i <= maxnum; i++) {      
        for (let k = 0; k < nums.length; k++) {
            if (array1[getDigit(nums[k], i)]) {
                array1[getDigit(nums[k], i)].push(nums[k]);
                // here I shift the corresponding number out of the array 'nums', which I add back later
                nums.shift()
            }
        } 
        for (let q = 0; q <= 9; q++) {
            for (let m = 0; m < array1[q].length; m++) {
                let temp = array1[q].shift;
                nums.unshift(temp);
                }
            }
        }
    }
    return nums
}
console.log(radixSort([8, 6, 1, 12]))

输出,其中值未按升序排序:

[ [Function: shift], [Function: shift], 1, 12 ]

函数getDigit()mostDigits()执行正常,因为我已经仔细检查过它们。问题出在 radixSort() 函数内部的某处。我根据Visualgo上的插图写了我的代码。

试一试:

// get digit at a specific location. 
// getDigit(12345,0) -> output: 5 
function getDigit(num1, i) {
    num1 = String(num1);
    num1 = num1.split('').reverse().join('');
    if (i>= num1.length) { return 0 }
    else { return parseInt(num1[i]) }
}


function mostDigits(array1) {
    if (array1.length == 0) {
        return 0
    }
    array1 = array1.map(x => String(x).length)
    return Math.max(...array1)
}
function radixSort(nums) {
    let maxnum = mostDigits(nums);
    for (let i = 0; i <= maxnum; i++) {      
      let array1 = Array.from({ length: 10 }, () => []);
        for (let k = 0; k < nums.length; k++) {
            if (array1[getDigit(nums[k], i)]) {
                array1[getDigit(nums[k], i)].push(nums[k]);
            }
        } 
        nums = array1.flat();
    }
    return nums
}
console.log(radixSort([8, 6, 1, 12, 44, 4]))