Advertisement
Guest User

Burbuja

a guest
Feb 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package programacion;
  2. /*@author GONZALO */
  3. public class burbuja {
  4.      public static void main (String args[]){
  5.          int[] array1 = {5220,2446,5577,9558,14415,2557,547,584,565,453,324,2344,321,323,323,122,432,321,322,345,345};
  6.          burbuja(array1);
  7.          for(int j = 0; j<array1.length;j++){
  8.             System.out.println(array1[j]);
  9.             }
  10.      }
  11.      static int[] burbuja( int array1[]){
  12.         int mayor = 0;//INICIALIZAMOS LA VARIABLE QUE OCUPARA EL MAYOR VALOR DEL ARRAY
  13.             for(int i = 0; i < array1.length-1;i++){// recorremos el array  
  14.                 if(array1[i] > array1[i+1]){//comprobamos que las posiciones contiguas son mayores o menores
  15.                     mayor = array1[i];//en caso de ser mayor la variable mayor toma el numero de la posicion i en esa instancia
  16.                     array1[i] = array1[i+1];//luego iguala la posicion i a la siguiente
  17.                     array1[i+1] = mayor;//y la posicion i+1 le da el valor de mayor ( el antiguo i)
  18.                     burbuja(array1);// se vuelve a llamar a la funcion hasta que se salga del if y este todo ordenado
  19.                 }
  20.             }
  21.         return array1;
  22.      }    
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement