Created
March 20, 2022 21:47
-
-
Save coproduto/2b664ebbf87145e174eaafa88c5e7aa4 to your computer and use it in GitHub Desktop.
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
const verificaOrdenacao = (array) => | |
array.every((elem, indice) => indice === array.length - 1 || elem <= array[indice + 1]); | |
// O algoritmo usado aqui se chama Embaralhamento de Durstenfeld | |
const embaralha = (array) => { | |
for (let i = array.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
[array[i], array[j]] = [array[j], array[i]]; | |
} | |
} | |
const ateOrdenar = (array) => { | |
let novoArray = [...array]; | |
do { | |
console.log(novoArray); | |
embaralha(novoArray); | |
} while(!verificaOrdenacao(novoArray)); | |
return novoArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment