Skip to content

Instantly share code, notes, and snippets.

@coproduto
Created March 20, 2022 21:47
Show Gist options
  • Save coproduto/2b664ebbf87145e174eaafa88c5e7aa4 to your computer and use it in GitHub Desktop.
Save coproduto/2b664ebbf87145e174eaafa88c5e7aa4 to your computer and use it in GitHub Desktop.
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