MergeSort

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Aug 10 05:38:00 EDT 2006


On Wed, 09 Aug 2006 12:50:11 -0700, ben81 wrote:

> Hi,
> 
> the following code is adopted PseudoCode from Introduction to
> Algorithms (Cormen et al). 

I'm assuming you are doing this as a learning exercise, because -- trust
me on this -- nothing you write in pure Python code will come within cooee
of the speed of Python's build-in sort method.

> For some reason it can't get it to work. I
> always get a index of out bounds exception or some weird result.

No, don't tell us what the weird result is. I love to guess!

Is it this?

ImportError: No module named listutil


Not that it matters, because listutil doesn't seem to be used in your code.


> Secondly I'd like to know how to write this more pythonic. TIA.

Document your code. Don't assume the reader will remember all the
details of Merge Sort. I certainly don't. It will also help you understand
how the algorithm works, which then will help you see what your code is
doing that it shouldn't (such as trying to merge empty lists).

Why are you appending 1001 to both sub-lists?


-- 
Steven D'Aprano 




More information about the Python-list mailing list