Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 5, 2020 03:22
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/2aa5ea61550f60b7c7f8a8868bdb2597 to your computer and use it in GitHub Desktop.
Save parzibyte/2aa5ea61550f60b7c7f8a8868bdb2597 to your computer and use it in GitHub Desktop.
def burbuja(arreglo):
# Calculamos la longitud del arreglo para tener un código más limpio
longitud = len(arreglo)
# Recorremos todo el arreglo
for i in range(longitud):
# Dentro del ciclo, volvemos a recorrerlo. Pero ahora hasta el penúltimo elemento
for indice_actual in range(longitud - 1):
indice_siguiente_elemento = indice_actual + 1
# Si el actual es mayor que el siguiente, ...
# Nota: para un orden inverso, cambia `>` por `<`
if arreglo[indice_actual] > arreglo[indice_siguiente_elemento]:
# ... intercambiamos los elementos
arreglo[indice_siguiente_elemento], arreglo[indice_actual] = arreglo[indice_actual], arreglo[indice_siguiente_elemento]
# No hace falta regresar nada, pues el arreglo ya fue modificado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment