Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 31, 2020 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/eee7c2d36015e566390f4860b4c56651 to your computer and use it in GitHub Desktop.
Save parzibyte/eee7c2d36015e566390f4860b4c56651 to your computer and use it in GitHub Desktop.
public class Main {
// https://parzibyte.me/blog
public static void main(String[] args) {
// El arreglo
int[] arreglo = {30, 28, 11, 96, -5, 21, 18, 12, 22, 30, 97, -1, -40, -500};
System.out.println("Imprimiendo arreglo antes de ordenar...");
for (int i : arreglo) {
System.out.printf("%d, ", i);
}
// Ordenamos. Recuerda: en Java los arreglos se pasan por referencia así que no necesitamos obtener
// el resultado, ya que dentro de la función se modifica al arreglo
// Más información: https://parzibyte.me/blog/2020/04/04/java-parametros-referencia-valor/
ordenarPorSeleccion(arreglo);
System.out.println("\nImprimiendo arreglo después de ordenar...");
for (int i : arreglo) {
System.out.printf("%d, ", i);
}
}
public static void ordenarPorSeleccion(int[] arreglo) {
for (int i = 0; i < arreglo.length - 1; i++) {
for (int j = i + 1; j < arreglo.length; j++) {
if (arreglo[i] > arreglo[j]) {
// ...intercambiarlos, es decir, mover el actual a la derecha y el de la derecha al actual
int temporal = arreglo[i];
arreglo[i] = arreglo[j];
arreglo[j] = temporal;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment