Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda