cardel

Boggosort

Jan 8th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function verificar(arreglo){
  3.   for(i = 0; i<arreglo.length-1; i++){
  4.  
  5.     if(arreglo[i]>arreglo[i+1]){
  6.       return false
  7.     }
  8.   }
  9.  
  10.   return true
  11.  
  12. }
  13.  
  14. function ordenar(arreglo){
  15.   while(!verificar(arreglo)){
  16.     posA = Math.ceil(Math.random()*(arreglo.length-1));
  17.  
  18.     posB = Math.floor(Math.random()*(arreglo.length-1));
  19.  
  20.     aux = arreglo[posA];
  21.     arreglo[posA] = arreglo[posB];
  22.     arreglo[posB] = aux;
  23.  
  24.  
  25.   }
  26.   return arreglo;
  27. }
  28.  
  29. arreglo = [4,5,1,2,3,9,8,10,-1];
  30. console.log(arreglo)
  31. console.log(ordenar(arreglo));
  32.  
Add Comment
Please, Sign In to add comment