Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 3, 2021 02:19
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/9e0c0d235c3a34404681c89d14261cfd to your computer and use it in GitHub Desktop.
Save parzibyte/9e0c0d235c3a34404681c89d14261cfd to your computer and use it in GitHub Desktop.
static void ordenarBurbuja(int[] arreglo)
{
for (int x = 0; x < arreglo.Length; x++)
{
// Recuerda que el -1 es porque no queremos llegar al final ya que hacemos
// un indiceActual + 1 y si fuéramos hasta el final, intentaríamos acceder a un valor fuera de los límites
// del arreglo
for (int indiceActual = 0; indiceActual < arreglo.Length - 1;
indiceActual++)
{
int indiceSiguienteElemento = indiceActual + 1;
// Si el actual es mayor que el que le sigue a la derecha...
if (arreglo[indiceActual] > arreglo[indiceSiguienteElemento])
{
int temporal = arreglo[indiceActual];
arreglo[indiceActual] = arreglo[indiceSiguienteElemento];
arreglo[indiceSiguienteElemento] = temporal;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment