Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package programacion;
- /*@author GONZALO */
- public class burbuja {
- public static void main (String args[]){
- int[] array1 = {5220,2446,5577,9558,14415,2557,547,584,565,453,324,2344,321,323,323,122,432,321,322,345,345};
- burbuja(array1);
- for(int j = 0; j<array1.length;j++){
- System.out.println(array1[j]);
- }
- }
- static int[] burbuja( int array1[]){
- int mayor = 0;//INICIALIZAMOS LA VARIABLE QUE OCUPARA EL MAYOR VALOR DEL ARRAY
- for(int i = 0; i < array1.length-1;i++){// recorremos el array
- if(array1[i] > array1[i+1]){//comprobamos que las posiciones contiguas son mayores o menores
- mayor = array1[i];//en caso de ser mayor la variable mayor toma el numero de la posicion i en esa instancia
- array1[i] = array1[i+1];//luego iguala la posicion i a la siguiente
- array1[i+1] = mayor;//y la posicion i+1 le da el valor de mayor ( el antiguo i)
- burbuja(array1);// se vuelve a llamar a la funcion hasta que se salga del if y este todo ordenado
- }
- }
- return array1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement