Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 9, 2019 04:50
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/eb7bee3ca2a8ffd2667c6c6c3dc5f4f5 to your computer and use it in GitHub Desktop.
Save parzibyte/eb7bee3ca2a8ffd2667c6c6c3dc5f4f5 to your computer and use it in GitHub Desktop.
void burbuja(int arreglo[], int longitud) {
for (int x = 0; x < longitud; x++) {
// El for solo va hasta la mitad, por eso longitud - x - 1
// 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 < longitud - x - 1;
indiceActual++) {
int indiceSiguienteElemento = indiceActual + 1;
// Si el actual es mayor que el que le sigue a la derecha...
if (arreglo[indiceActual] > arreglo[indiceSiguienteElemento]) {
// ...intercambiarlos, es decir, mover el actual a la derecha y el de la derecha al actual
intercambiar(&arreglo[indiceActual], &arreglo[indiceSiguienteElemento]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment