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

218
Vistas
Why does my sort implementation not work?

I'm trying to sort an integer array without using sort function. I know there are other solutions available on Stack Overflow. I want to know what is wrong with my code. It performs ascending sort except for the first number in the array.

let arr = [2,4,5,1,3,7];
let iterable = true;
let iterationCount = 0;

while(iterable) {
 for(var i=iterationCount;i<=arr.length;i++) {
    if (arr[i] > arr[i+1]) {
      let temp=arr[i];
      arr[i]=arr[i+1];
      arr[i+1]=temp;
    }
 }
 iterationCount++;
 if (iterationCount == arr.length) {
    iterable = false;
 }
}

console.log(arr)

The output is [2, 1, 3, 4, 5, 7] while it should be [1, 2, 3, 4, 5, 7].

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

0

  1. Each time it goes around the inner loop, it moves each element zero or one spaces to the left.
  2. Each time it goes around the outer loop, it ignores one more element from the left hand side.

This means that it assumes that after one loop the left most element is the smallest element (and after two loops, the two left most elements are the two smallest).

However, because of (1) the 1 will have moved from position 3 to position 2 but it needs to be in position 0.


Wikipedia has a selection of popular sorting algorithms you should read up on if you are implementing sorting from scratch.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could change the outer loop for keeping the last index for checking and iterate until before the last index, because in the first inner loop, the max value is now at the greatest index and any further iteration do not need to check the latest last item.

let array = [2, 4, 5, 1, 3, 7],
    iterationCount = array.length;

while (iterationCount--) {
    for (let i = 0; i < iterationCount; i++) {
        if (array[i] > array[i + 1]) {
            let temp = array[i];
            array[i] = array[i + 1];
            array[i + 1] = temp;
        }
    }
}

console.log(array);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I fixed the for loop to always start at 0, then it works.

let arr = [2, 4, 5, 1, 3, 7];

for (let j = 0; j < arr.length; j++) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] > arr[i + 1]) {
      const temp = arr[i];
      arr[i] = arr[i + 1];
      arr[i + 1] = temp;
    }
  }
}

console.log(arr)

EDIT: I made the whole thing a little shorter

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