Created
December 26, 2019 06:26
-
-
Save parzibyte/98393bd3cc96ddc9a3a9ec1a7fc94400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
/* | |
* Método de burbuja en Java para ordenar arreglos | |
* | |
* @author parzibyte | |
* @see https://parzibyte.me/blog | |
* */ | |
public class MetodoBurbuja { | |
public static void main(String[] args) { | |
int numeros[] = {1, 9, 23, 4, 55, 100, 1, 1, 23}; | |
System.out.println("Antes del método de la burbuja: " + Arrays.toString(numeros)); | |
burbuja(numeros); | |
System.out.println("Después del método de la burbuja: " + Arrays.toString(numeros)); | |
String[] nombres = {"Leon", "Chris", "Jill", "Wesker", "Ada"}; | |
System.out.println("Antes del método de la burbuja: " + Arrays.toString(nombres)); | |
burbuja(nombres); | |
System.out.println("Después del método de la burbuja: " + Arrays.toString(nombres)); | |
} | |
private static void burbuja(int[] arreglo) { | |
for (int x = 0; x < arreglo.length; x++) { | |
// Aquí "y" se detiene antes de llegar | |
// a length - 1 porque dentro del for, accedemos | |
// al siguiente elemento con el índice actual + 1 | |
for (int y = 0; y < arreglo.length - 1; y++) { | |
int elementoActual = arreglo[y], | |
elementoSiguiente = arreglo[y + 1]; | |
if (elementoActual > elementoSiguiente) { | |
// Intercambiar | |
arreglo[y] = elementoSiguiente; | |
arreglo[y + 1] = elementoActual; | |
} | |
} | |
} | |
} | |
private static void burbuja(String[] arreglo) { | |
for (int x = 0; x < arreglo.length; x++) { | |
// Aquí "y" se detiene antes de llegar | |
// a length - 1 porque dentro del for, accedemos | |
// al siguiente elemento con el índice actual + 1 | |
for (int y = 0; y < arreglo.length - 1; y++) { | |
String elementoActual = arreglo[y], | |
elementoSiguiente = arreglo[y + 1]; | |
if (elementoActual.compareTo(elementoSiguiente) > 0) { | |
// Intercambiar | |
arreglo[y] = elementoSiguiente; | |
arreglo[y + 1] = elementoActual; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment