Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 22, 2021 22:52
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/b51c8a56718a14dcfdba773fe2ee1bb6 to your computer and use it in GitHub Desktop.
Save parzibyte/b51c8a56718a14dcfdba773fe2ee1bb6 to your computer and use it in GitHub Desktop.
def merge(left, right):
merged_list = []
indice_de_izquierda = 0
indice_de_derecha = 0
indice_arreglo_ordenado = 0
while indice_de_izquierda < len(left) and indice_de_derecha < len(right):
valor_izquierda = left[indice_de_izquierda]
valor_derecha = right[indice_de_derecha]
# Accedemos a 0 porque en 0 está el nombre (en 1 está el ID)
if valor_izquierda[0] <= valor_derecha[0]:
# El de la izquierda es menor, entonces lo ponemos primero
merged_list.append(valor_izquierda)
# Y aumentamos en 1 el valor de la izquierda
indice_de_izquierda += 1
else:
merged_list.append(valor_derecha)
indice_de_derecha += 1
# Sin importar lo que hayamos movido, aumentamos el índice del actual
indice_arreglo_ordenado += 1
# Hasta aquí puede que el índice izquierdo o derecho hayan llegado a su fin, pero no ambos. Entonces
# nos aseguramos de recorrerlos a ambos hasta el final
while indice_de_izquierda < len(left):
merged_list.append(left[indice_de_izquierda])
indice_de_izquierda += 1
while indice_de_derecha < len(right):
merged_list.append(right[indice_de_derecha])
indice_de_derecha += 1
return merged_list
def merge_sort(lista):
longitud = len(lista)
mitad = longitud//2 # El doble / es para dividir y redondear hacia abajo
# Condición de salida de recursividad es que el arreglo mida 1 o 0
if longitud <= 1:
return lista
left = merge_sort(lista[:mitad])
right = merge_sort(lista[mitad:])
return merge(left, right)
def nombre_ascendente(self, pokemones):
result = []
for id_pokemon in pokemones:
result.append((pokemones.get(id_pokemon)["nombre"], id_pokemon))
result = merge_sort(result)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment