Last active
April 27, 2022 12:34
-
-
Save 4lessandrodev/a8205b62848e89a3b9c966194c9adbd7 to your computer and use it in GitHub Desktop.
Ordenar vetor de números com typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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