pythonnumpynp.argsort

How is the pivot chosen in numpy.argsort?


I want to benchmark np.argsort by comparing different sorting algorithms, that are ‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’ available in the np.argsort documentation. I was wondering how was the pivot chosen in the i.e. quicksort.

I found the source file for np.sort and the choice of pivot is clear there. However, I cannot find the file describing what is happening under the hood of the np.argsort. The reasonable assumption would be that the methods are performing the same, however, I would want to somehow verify it.


Solution

  • When calling np.argsort(kind='quicksort') the call gets dispatched to PyArray_ArgSort and then to npy_aquicksort (C source links):

    This also includes a choice of pivot identical to your link to np.sort:

    pm = pl + ((pr - pl) >> 1);
    

    Without having looked into it further, I assume this chooses the center element of the current partition as pivot.