Shreyas Gune Shreyas Gune - 2 years ago 149
Python Question

Bottom Up MergeSort using Python

I've spent countless hours trying to do this. Could anyone point out my mistake?

is just a list,
is an empty list of size

is basically

a = [6,5,4,3,2,1]
print 'unsorted:',a
z = len(a)
tmp = range(len(a))

Here's my sort function:

def sort(a,tmp):
left =p
middle = p+width
right = p+2*width
p = p+2*width
print '\n'

and here's merge function :

def merge(a,iLeft,iMiddle,iRight,tmp):
i = iLeft
j = iMiddle
k = iLeft
print iLeft,iMiddle,iRight,'[',i,j,k,']'
#print i ,j, k,'\n\n'

while(i<iMiddle or j<iRight):
if(i<iMiddle and j<iRight):
i += 1
k += 1
j += 1
k += 1

elif (i==iMiddle):
tmp[k] = a[j]
j += 1
k += 1
elif (j==iRight):
tmp[k]= a[i]
i += 1
k += 1

[This Visualization] might help. I still don't know why it's acting this way.

I'm not sure where I am going wrong? Is it the indentation or the looping?

Output ::
unsorted: [6, 5, 4, 3, 2, 1]
0 1 2 [ 0 1 0 ]
2 3 4 [ 2 3 2 ]
4 5 6 [ 4 5 4 ]

0 2 4 [ 0 2 0 ]
4 6 8 [ 4 6 4 ]
Traceback (most recent call last):
File "", line 45, in <module>
File "", line 14, in sort
File "", line 27, in merge
IndexError: list index out of range

This visualization may help. I still don't know why this is happening.

Answer Source

Although it was a valiant effort the primary mistake you made was with the nested flow control approach -- the human mind can only handle so many nested while-loops. Additionally, the fact that the merge function modifies a in place makes it extremely difficult to ascertain what is happening. Even if you have an amazing mind that can keep track of all that action, save that brain energy for tackling the problem rather than flow control!

In general, you want to do your best to keep your program flat -- avoid nested flow control. Additionally, unless you are doing dedicated object-oriented-programming, instead of modifying a in place you should attempt to return a specific value.

Here is another approach to mergesort that tries to keep things more flat and explicit:

def merge(merged, a, b):

    if a and b:
        return merge(merged + [min(a[0], b[0])],
                     a[1:] if min(a[0], b[0]) == a[0] else a,
                     b[1:] if min(a[0], b[0]) == b[0] and not a[0] == b[0] else b

    elif a and not b and len(a) > 1:
        return merged + merge([], a[:len(a)/2], a[len(a)/2:])
    elif a and not b:
        return merged + a
    elif b and not a  and len(b) > 1:
        return merged + merge([], b[:len(b)/2], b[len(b)/2:])
    elif b and not a:
        return merged + b
        return merged

def mergesort(lst):

    if not lst:
        return []
    elif len(lst) == 2:
        return [min(lst), max(lst)]
    elif len(lst) == 1:
        return lst
        return merge([], mergesort(lst[:len(lst)/2]), mergesort(lst[len(lst)/2:]))

This effort to keep things explicit and minimize flow control structures is common in a style of programming known as functional programming. There's a great free book on it that you can access here:

Realize this might not be the exact answer you're looking for but I hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download