Last active
March 18, 2018 04:09
-
-
Save Macorreag/c69b92b2ef5b090c401233fb338bbc29 to your computer and use it in GitHub Desktop.
Algoritmos De ordenamiento Implementaciones Explicadas
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 mergeSort(alist): | |
print("Splitting ",alist) | |
#Si la lista es superior a 1 se saldra no hay mas divisiones | |
if len(alist)>1: | |
#Divide en 2 la longitud de la lista y toma la parte entera | |
mid = len(alist)//2 | |
#parte izquierda de la lista toma desde el inicio de la lista hasta la mitad que determino antes | |
lefthalf = alist[:mid] | |
#toma desde la mitad que determino antes y divide hasta el final de la lista | |
righthalf = alist[mid:] | |
#Truco usa recursividad para dividir mas | |
mergeSort(lefthalf) | |
mergeSort(righthalf) | |
i=0 | |
j=0 | |
k=0 | |
while i < len(lefthalf) and j < len(righthalf): | |
#Verifica cual es el menor elemento de las dos listas y lo une a la lista principal | |
if lefthalf[i] < righthalf[j]: | |
alist[k]=lefthalf[i] | |
i=i+1 | |
else: | |
alist[k]=righthalf[j] | |
j=j+1 | |
k=k+1 | |
#Si una lista es mas grande que la otra,pega esta a la principal | |
while i < len(lefthalf): | |
alist[k]=lefthalf[i] | |
i=i+1 | |
k=k+1 | |
while j < len(righthalf): | |
alist[k]=righthalf[j] | |
j=j+1 | |
k=k+1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment