Mergesort problem

Deborah Swanson python at deborahswanson.net
Wed Dec 21 21:27:46 EST 2016


> On Thu, Dec 22, 2016 at 11:55 AM, Deborah Swanson 
> <python at deborahswanson.net> wrote:
> > The problem is that while mergeSort puts the list ls in 
> perfect order, 
> > which I can see by looking at result on merge's final return to 
> > mergeSort, and at the left and the right once back in 
> mergeSort. Both 
> > the left half and the right half are in order. But the list 
> L is still 
> > in its original order, and after mergeSort completes, ls is 
> still in 
> > its original order. Maybe there's some bonehead error causing this, 
> > but I just can't see it.
> >
> 
> Your analysis is excellent. Here's what happens: When you 
> merge-sort, you're always returning a new list (either 
> "return L[:]" or "result = []"), but then you call it like this:
> 
>     # sort: Description only, to make hyperelinks & find duplicates
>     mergeSort(ls)
> 
> This calls mergeSort, then drops the newly-sorted list on the 
> floor. Instead, try: "ls = mergeSort(ls)".
> 
> Thank you for making it so easy for us!
> 
> ChrisA

"ls = mergeSort(ls)" works perfectly!

I can see why now, but I'm not sure how long I would have knocked my
head against it before I saw it on my own. It must take awhile to
develop an eye for these things.

So thank you from the bottom of my heart! I do have a future in python
coding planned, but right now I need to find the cheapest nice little
house to move to, and this sorting problem was a major roadblock! The
webpage titles and urls are from Craigslist, soon to be joined by many
other fields, but I just couldn't get past this one problem.




More information about the Python-list mailing list