Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions sorting/merge2lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

def mergeboth(a, b):
index = 0
Copy link

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?

length = len(a) + len(b)
c = []
if len(a) == 0:
print('----- Final result -----')
print(b)
print('------------------------')
return b
if len(b) == 0:
print('----- Final result -----')
print(a)
print('------------------------')
return a
while len(c) < length-1:
if a[index] <= b[index]:
print(str(a[index]) + ' minor than ' + str(b[index]))
if len(c) > 1 and a[index] < c[-1]:
c.insert(-1, a[index])
else:
c.append(a[index])
c.append(b[index])
print('Current C array')
print(c)
else:
print(str(a[index]) + ' major than ' + str(b[index]))
if len(c) > 1 and b[index] < c[-1]:
c.insert(-1, b[index])
else:
c.append(b[index])
c.append(a[index])
print('Current C array')
print(c)
if index == len(a) -1:
if index < len(b)-1:
print('there are elements left from: ' + str(index) )
print('elements left from ' + str(index))
print(b[index+1:])
c.extend(b[index+1:])
print('Check lengths')
print(length, len(c))
else:
if index == len(b)-1:
print('there are elements left from: ' + str(index) )
print('elements left from ' + str(index))
print(a[index+1:])
c.extend(a[index+1:])
print('Check lengths')
print(length, len(c))
else:
index += 1
print('----- Final result -----')
print(c)
print('------------------------')
return c

def test1():
print('starting test1')
a = [1,2,5,8,8]
b = [3,6,7,7]
assert mergeboth(a, b) == [1, 2, 3, 5, 6, 7, 7, 8, 8]
print('------------------------')


def test2():
print('starting test2')
f = [1,4,6,8,9]
g = [2,5,7]
assert mergeboth(f,g) == [1, 2, 4, 5, 6, 7, 8, 9]
print('------------------------')


def test3():
print('starting test3')
d = [2,6,8]
e = [1,7,9]
assert mergeboth(d,e) == [1, 2, 6, 7, 8, 9]
print('------------------------')

def test4():
print('starting test4')
a = [1,3,5,7]
b = [2,4,6]
assert mergeboth(a,b) == [1, 2, 3, 4, 5, 6, 7]
print('------------------------')

def test5():
print('starting test5')
a = [1,2,9]
b = [5,7,8]
assert mergeboth(a,b) == [1, 2, 5, 7, 8, 9]
print('------------------------')

def test6():
print('starting test6')
a = [1,2]
b = []
assert mergeboth(a,b) == [1, 2]
print('------------------------')

def test7():
print('starting test7')
a = []
b = []
assert mergeboth(a,b) == []
print('------------------------')

def test8():
print('starting test8')
a = []
b = [1,2]
assert mergeboth(a,b) == [1, 2]
print('------------------------')

if __name__ == "__main__":
test1()
test2()
test3()
test4()
test5()
test6()
test7()
test8()
42 changes: 42 additions & 0 deletions sorting/mergesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@


def merge_sort(original_list):
print('entering divide')
if len(original_list) > 1:
Copy link

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.

middle_index = len(original_list)//2
left = original_list[:middle_index]
right = original_list[middle_index:]
merge_sort(left)
merge_sort(right)
print('i am back!')
left_index, right_index, original_index = 0, 0, 0
while left_index < len(left) and right_index < len(right):
if left[left_index] <= right[right_index]:
original_list[original_index] = left[left_index]
left_index += 1
else:
original_list[original_index] = right[right_index]
right_index += 1
original_index += 1
print('we have just put two values in order from both halves')
print(right[right_index-1], left[left_index-1])

while left_index < len(left):
print('this means that right half is out of stock')
print(right, right_index, left, left_index)
original_list[original_index] = left[left_index]
left_index += 1
original_index += 1

while right_index < len(right):
print('this means that left half is out of stock')
print(right, right_index, left, left_index)
original_list[original_index] = right[right_index]
right_index += 1
original_index += 1

if __name__ == '__main__':
list_to_sort = [5,4,2,7,9,1]
merge_sort(list_to_sort)
print('now sorted')
print(list_to_sort)