Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

72
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda