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. The algorithm guarantees that items in partitions smaller than a correctly sorted item come before that item. Otherwise, they are put behind that item.