[Numpy-discussion] sorting -inf, nan, inf

A. M. Archibald peridot.faceted at gmail.com
Tue Sep 19 16:15:24 CDT 2006

On 19/09/06, Tim Hochberg <tim.hochberg at ieee.org> wrote:

> I'm not sure where the breakpoint is, but I was seeing failures for all
> three sort types with N as high as 10000. I suspect that they're all
> broken in the presence of  NaNs.  I further suspect you'd need some
> punishingly slow n**2 algorithm to be robust in the presence of NaNs.

Not at all. Just temporarily make NaNs compare greater than any other
floating-point value and use quicksort (say). You can even do this for
python lists without much trouble.

That's actually a viable suggestion for numpy's sorting, although it
would be kind of ugly to implement: do a quick any(isnan(A)), and if
not, use the fast stock sorting algorithms; if there is a NaN
somewhere in the array, use a version of the sort that has a tweaked
comparison function so the NaNs wind up at the end and are easy to
trim off.

But the current situation, silently returning arrays in which the
non-NaNs are unsorted, is really bad.

A. M. Archibald

More information about the Numpy-discussion mailing list