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

71
Vistas
Is this considered a selection sort?

I was practicing a while ago and came across selection sort. After some research across difference sources, there are some that declare an array then delete the current min location while others swap within the array

I tried to use ES6 for some trivial functions, did not use map since I wanted to understand the loop on a whiteboard.

Is this considered a selection sort?

selectionSortNoSwap = list)= => {
  const result = [];

  for (let i = 0; i < list; i++) {
    const min = Math.min(...list);
    const minIndex = list.indexOf(min);

    result.push(min);
    list.splice(minIndex, 1);
  }

  return result;
};

selectionSortNoSwap([3, 5, 2, 1, 4]);

Thank you

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

0

No. True selection sort sorts in-place, rather than creating another array. That is, given:

[3, 5, 2, 1, 4]

after the first iteration, a selection sort should produce the following data structure in memory:

[1, 5, 2, 3, 4]

where the 1 and 3 have been exchanged - and not

[3, 5, 2, 4]
[1]

If elements are not swapped with each other, it's not selection sort.

To sort in-place, you'd need something like

const selectionSort = (list) => {
  for (let i = 0; i < list.length; i++) {
    // for convenience - or use a for loop 
    const min = Math.min(...list.slice(i));
    const minIndex = list.indexOf(min, i);
    [list[i], list[minIndex]] = [list[minIndex], list[i]];
  }
  return list;
};

console.log(selectionSort([3, 5, 2, 1, 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