-
-
Save parzibyte/3df1b555ef16730bb28ee5e9eec43c72 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
""" | |
https://parzibyte.me/blog | |
""" | |
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 | |
# Modo de uso. | |
mi_arreglo = [3, 4, 1, 2, 3, 7, 55, 34, 43, 3] | |
print("Original: ") | |
print(mi_arreglo) | |
burbuja(mi_arreglo) | |
print("Ordenado: ") | |
print(mi_arreglo) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment