Skip to content

Instantly share code, notes, and snippets.

@4lessandrodev
Last active April 27, 2022 12:34
Show Gist options
  • Save 4lessandrodev/a8205b62848e89a3b9c966194c9adbd7 to your computer and use it in GitHub Desktop.
Save 4lessandrodev/a8205b62848e89a3b9c966194c9adbd7 to your computer and use it in GitHub Desktop.
Ordenar vetor de números com typescript
function ordenarVetorNumerico (vetorNumerico: number[]): number[] {
let indiceMenorValor = 0;
let auxiliar = 0;
const vetorCopia = [...vetorNumerico];
const tamanhoDoVetor = vetorNumerico.length;
const quantidadeDeNumeros = tamanhoDoVetor - 1;
for (let indiceA=0; indiceA<=quantidadeDeNumeros; indiceA++){
indiceMenorValor = indiceA;
for (let indiceB=indiceA+1; indiceB<=tamanhoDoVetor; indiceB++){
if (vetorCopia[indiceB] < vetorCopia[indiceMenorValor]){
indiceMenorValor = indiceB;
}
}
auxiliar = vetorCopia[indiceMenorValor];
vetorCopia[indiceMenorValor] = vetorCopia[indiceA];
vetorCopia[indiceA] = auxiliar;
}
return vetorCopia;
}
const arr = [5,2,3,4,1,9,11,0,3];
const ordenacao = ordenarVetorNumerico(arr);
console.log(ordenacao);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment