I am trying to implement the mergesort algorithm in Python. My input, file.txt, is of the form:
1
2
3
4
5
55
60
82
19
However, my output becomes (notice the second element of the list):
[['1'], ['19'], ['2'], ['3'], ['4'], ['5'], ['55'], ['60'], ['82']]
Can someone please explain why this implementation of mergesort fails to place the 19 into its proper location in the Python list?
import csv
list = []
with open('file.txt') as f:
lines = csv.reader(f, delimiter='\n')
for line in lines:
list.append(line)
def mergesort(list):
mid = len(list)//2
lft, rgt = list[:mid], list[mid:]
if len(lft) > 1: lft = mergesort(lft)
if len(rgt) > 1: rgt = mergesort(rgt)
res = []
while lft and rgt:
if lft[-1] >=rgt[-1]:
res.append(lft.pop())
else:
res.append(rgt.pop())
res.reverse()
return (lft or rgt) + res
print mergesort(list)