Python Forum
Why won't my merge sort will not work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why won't my merge sort will not work?
#5
Two other nitpicks.

There is no reason for the else: If len(ls) == 1 you will never get past the "return ls". There is no reason for the protection provided by the else, and no reason to lose the indent space.
def ms(ls):
  if len(ls)==1: #base case
    return ls
  else:
    mid = len(ls)//2
    l = ms(ls[:mid])
    r = ms(ls[mid:])
    nu=[] #return list
Indent should be 4, not 2. You code would be easier to read if the blocks were easier to see.

Never use lower case L or upper case O as variable names. It is too easy to mistake this as 1 or 0.

Use better function and variable names. This should clearly be called mergesort or merge_stort. The left and right sides should be called left and right, not l and r. When you begin using a linter it is going to complain, A lot. May as well develop good habits early on.

https://www.python.org/dev/peps/pep-0008/

And don't put multiple statements on one line. Not even beak.
if len(r)==0: break
Reply


Messages In This Thread
Why won't my merge sort will not work? - by ggg - Feb-02-2021, 11:28 PM
RE: Why won't my merge sort will not work? - by ggg - Feb-03-2021, 02:43 AM
RE: Why won't my merge sort will not work? - by deanhystad - Feb-03-2021, 05:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,397 Apr-19-2022, 06:50 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020