-
-
Save parzibyte/2aa5ea61550f60b7c7f8a8868bdb2597 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
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