Skip to content

merge 2 sorted lists with simple tests#8

Open
maesbrisa wants to merge 5 commits into
masterfrom
feature/rise_repo
Open

merge 2 sorted lists with simple tests#8
maesbrisa wants to merge 5 commits into
masterfrom
feature/rise_repo

Conversation

@maesbrisa

Copy link
Copy Markdown
Owner

No description provided.

Comment thread sorting/merge2lists.py
@@ -0,0 +1,124 @@

def mergeboth(a, b):
index = 0

Copy link
Copy Markdown

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?

Comment thread sorting/mergesort.py Outdated
print('------------------------')
return a
while len(sorted_list) < length-1:
if list_to_sort[index] <= b[index]:

Copy link
Copy Markdown

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?

@Juanmihd Juanmihd Oct 5, 2020

Copy link
Copy Markdown

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

Comment thread sorting/mergesort.py Outdated
Comment on lines +85 to +87
divide(temp_list, list_to_sort)
print(temp_list)
merge(temp_list, len(list_to_sort))

Copy link
Copy Markdown

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

Comment thread sorting/mergesort.py
c.extend(a[index+1:])
print('Check lengths')
print(length, len(c))
if len(original_list) > 1:

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants