-
Notifications
You must be signed in to change notification settings - Fork 0
merge 2 sorted lists with simple tests #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| @@ -0,0 +1,124 @@ | |||
|
|
|||
| def mergeboth(a, b): | |||
| index = 0 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Veo que estás usando el mismo indice para ambos vectores.
Si usas distintos indices para cada vector, te permite insertar uno, y mover su indice, para comprobar a[1] con b[0], por ejemplo, y, probablemente, insertar a[1] en lugar de b[0], y luego comparar a[2] con b[0].
Quieres intentarlo de esa forma?
…can be a good idea
sorting/mergesort.py
Outdated
| print('------------------------') | ||
| return a | ||
| while len(sorted_list) < length-1: | ||
| if list_to_sort[index] <= b[index]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigues usando el mismo indice para las dos listas a mergear, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aqui te aconsejaria usar un index_a y luego index_b para mergear dos vectores que suponer ordenados, creo que se podría hacer algo así, usando dos indices, no?
index_a, index_b, index_c = 0
// fusionar mientras los dos tengan elementos al mismo tiempo
while(index_a < length(a) && index_b < length(b))
if a[index_a] <= b[index_b]
c[index_c] = a[index_a]
index_a += 1
else
c[index_c] = b[index_b]
index_b += 1
index_c +=1
// ahora a o b ya están vacios, si hay elementos de b, metelos todos al final
while(index_b < length(b))
c[index_c] = b[index_b]
index_b += 1
index_c +=1
// ahora a o b ya están vacios, si hay elementos de a, metelos todos al final
while(index_a < length(a))
c[index_a] = a[index_a]
index_a += 1
index_c +=1
sorting/mergesort.py
Outdated
| divide(temp_list, list_to_sort) | ||
| print(temp_list) | ||
| merge(temp_list, len(list_to_sort)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Veo que estas haciendo primero una fase de dividir todo, y luego mergear todo, creo que el merge sort es más en plan (en pseudocodigo)
mergesort(VectorDesordenado):
Conseguir PrimerMitad y SegundaMitad de VectorDesordenado,
Llamar a mergesort(PrimeraMitad) // ahora primeraMitad esta ordenado
Llamar a mergesort(SegundaMitad) // ahora segundaMitad esta ordenado
Fusionar PrimeraMitad y SegundaMitad y ponerlo en VectorOrdenado
Devolver VectorOrdenado
| c.extend(a[index+1:]) | ||
| print('Check lengths') | ||
| print(length, len(c)) | ||
| if len(original_list) > 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick:
No se si esto se puede hacer en paiton, pero, podrías probar algo como un early return?
if(len(original_list) <= 1):
return original_list
Asi te evitas tener todo tu codigo indexado.
No description provided.