sorting a list and counting interchanges

Raymond Hettinger vze4rx4y at verizon.net
Wed Apr 6 21:02:25 EDT 2005


[Jordan Rastrick]
> Unless I'm mistaken, this doesnt quite work, because it switches the
> parity of phase every time a comparison is made, rather than every time
> a swap is made. So:
>
> # <untested>
> phase = 1
> def mycmp(x,y):
>    global phase
>    c = cmp(x,y)
>    if c > 0: # i.e. a swap will be performed in the sort
>        phase = -phase
>    return c

You're right.  An important test was omitted.


Raymond





More information about the Python-list mailing list