Jump to content

Python: Merge Sort Error

ZacoAttaco
Go to solution Solved by Sauron,

you can use the floor function to ensure the index is an integer:

import math
#...
m = math.floor((l+r-1)/2)

it's weird they would leave this in a tutorial though

Hi all, still very new to Python so forgive me.

 

I'm trying to program a script to implement a Merge Sort on a pre-determined array. I've been following the example on this website https://www.geeksforgeeks.org/merge-sort/

 

After following the example it still wasn't working, so then I just selected all the code and copied into Sublime to further troubleshoot. When I run it on the Sublime, it doesn't work either.

 

This is a snippet of code from geeksforgeeks

def merge(arr, l, m, r): 
    n1 = m - l + 1
    n2 = r- m 
  
    # create temp arrays RUN-TIME ERROR OCCURS HERE
    L = [0] * (n1) 
    R = [0] * (n2) 
    ...
    
def mergeSort(arr,l,r): 
    if l < r: 
  
        # Same as (l+r)/2, but avoids overflow for 
        # large l and h 
        m = (l+(r-1))/2 #FIRST FLOATING POINT RESULT
  
        # Sort first and second halves 
        mergeSort(arr, l, m) 
        mergeSort(arr, m+1, r) 
        merge(arr, l, m, r) 

arr = [12, 11, 13, 5, 6, 7] 
n = len(arr) 
print ("Given array is") 
for i in range(n): 
    print ("%d" %arr[i]),

mergeSort(arr,0,n-1) 
print ("\n\nSorted array is") 
for i in range(n): 
    print ("%d" %arr[i]), 

The issue is it's trying to multiply the size of a list by a floating point integer, which obviously won't work.

 

Clearly, floating point results occur earlier on but on geekforgeeks IDE the issue is non existant, it doesn't seem to process them as floating point. When I print those values on the IDE, instead of being n1 == 0.75 and n2 == 0.75, both n1 and n2 == 1.

 

So again, I am still very new, but am I missing something?

Why would they put that as an example if it doesn't work?

Are they using some sort of rounding that I'm not aware of?
 

Thanks in advance,

Zac.

 

Link to comment
Share on other sites

Link to post
Share on other sites

you can use the floor function to ensure the index is an integer:

import math
#...
m = math.floor((l+r-1)/2)

it's weird they would leave this in a tutorial though

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Sauron said:

you can use the floor function to ensure the index is an integer:


import math
#...
m = math.floor((l+r-1)/2)

it's weird they would leave this in a tutorial though

Thanks for the reply, this wasn't in the example code though was it? Unless I missed it.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ZacoAttaco said:

Thanks for the reply, this wasn't in the example code though was it? Unless I missed it.

No, I added it. Glad to help ^^

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Sauron said:

No, I added it. Glad to help ^^

Sweet thanks for the help mate, probably going to stay away from that site from now on.

 

Edit: Applied the fix and worked like a charm, thanks again.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Sauron said:

it's weird they would leave this in a tutorial though

The sample code is written in Python 2 where '/' is integer division. To accomplish the same thing in Python 3 you can use '//'.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×