O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Partial sorting via selection for fast median with the partition() function

The partition() subroutine does partial sorting. This should be less work than normal sorting.

Note

Refer to http://en.wikipedia.org/wiki/Partial_sorting for more information. A useful scenario is selecting the top five (or some other number) items of a group. Partial sorting doesn't preserve the right order within the set of the top elements.

The first parameter of the subroutine is the input array to sort. The second parameter is an integer or a list of integers corresponding to the indices of the array elements. The partition() subroutine sorts items at those indices correctly. One specified index gives two partitions. Multiple indices result in more than two partitions. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required