Guest User

boggo_sort.js

a guest
Jun 15th, 2021
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function verificar(arreglo) {
  2.   for(let i=0; i<arreglo.length; i++) {
  3.     if(arreglo[i] > arreglo[i+1]) return true
  4.   }
  5.   return false
  6. }
  7.  
  8. function ordenar(arreglo) {
  9.   while((verificar(arreglo))){
  10.     let posA = Math.ceil(Math.random()*(arreglo.length-1));
  11.     let posB = Math.floor(Math.random()*(arreglo.length-1));
  12.     let aux = arreglo[posA];
  13.     arreglo[posA] = arreglo[posB];
  14.     arreglo[posB] = aux;
  15.   }
  16.   return arreglo;
  17. }
  18.  
  19. let arreglo = [3,2,1,4,5];
  20. console.log(ordenar(arreglo));
Advertisement
Add Comment
Please, Sign In to add comment