Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Here is my code:

// 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]))

The output, in which the values weren’t sorted in ascending order:

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

The functions getDigit() and mostDigits() execute normally as I have double checked them. The problem is somewhere inside the radixSort() function. I wrote my code based on the Illustration on Visualgo.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try it :

// 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]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading