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
This while loop for my sorting algorithm doesn't seem to exit, why?

I am coding in javascript and am trying to make a sorting algorithm, and my while loop doesnt seem to exit, anybody know why?

let sort = true
let length = 0
let i = 1
function multiSort(n) {
    length = n.length-1
    while (sort=true) {
        sort = false
        if (n[i]>n[i+1]) {
            [n[i],n[i+1] = n[i+1],n[i]]
            i += 1
            sort=true
            if (i = length) {
                i = 1
            }
            console.log(i)
        }
    }
    return n
}
console.log("Final Product: ", multiSort([3,2,5,1,4]), " Iterations: ", i)

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

0

You have a couple problems here.

As mentioned your using javascript incorrectly. You should spend some time learning about how operators and comparisons work. You can read about them here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#assignment_operators

  1. Your while statement will never be false.
  2. n[i+1] in your compare statement can be undefined so will be false when it shouldn't be checked.

I have re-written your algorithm using a for loop instead of a while loop since we get a free iterator when we set up the loop.

// We keep track of the iterations outside of the function
let iterations = 0

function multiSort(n) {
    iterations++
    
    // We set sort to false, if our conditions are not met we won't need to keep sorting
    let sort = false
    
    // Loop through every item in the array
    for(let i = 0; i < n.length; i++) {
        let next = n[i+1]
        // If the current item is larger than the next swap their positions. We also have to check for undefined here since the end of the array can't compare to anything.
        if(n[i] > next && next !== undefined) {
            // Set sort to true since at least one item need to be sorted. This will not be set if all items in the array are in sequence.
            sort = true
            n[i+1] = n[i]
            n[i] = next
        }
    }
    
    // If we set sort to true re-run everything with the current array.
    if(sort) {
        multiSort(n)
    }
    
    // return the final array
    return n
}

console.log("Final Product: ", multiSort([3,2,5,1,4]), " Iterations: ", iterations)

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