#10836·numba

Add `kind='heapsort'` to `ndarray.sort` and `argsort`

Author: swap357Created Sep 14, 2026Updated Sep 15, 2026
Labelsfeature_requestperformanceperformance - run time

After #10703 Numba supports 'quicksort', 'mergesort', and 'stable', and still rejects 'heapsort' at compile time.

This is a feature request for Numba to add a JIT heapsort and accept that kind on ndarray.sort. Currently it errors with -

>>> import numpy as np
>>> np.__version__
'2.6.0.dev0+git20260826.4a0e6cb'
>>> import numba
>>> numba.__version__
'0.68.0dev0+204.g3566cf768'
>>> from numba import jit
>>> 
>>> @jit
... def sort_heap(a):
...     a.sort(kind='heapsort')
...     
>>> sort_heap(np.array([3, 1, 4, 1, 5]))
Traceback (most recent call last):
  File "<python-input-16>", line 1, in <module>
    sort_heap(np.array([3, 1, 4, 1, 5]))
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/swap357/Documents/dev/numba/numba/core/dispatcher.py", line 422, in _compile_for_args
    error_rewrite(e, 'typing')
    ~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/swap357/Documents/dev/numba/numba/core/dispatcher.py", line 363, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
Unsupported "kind": 'heapsort'

Follow-up: once this exists, the default quicksort path can use it as an introsort fallback (#5373). This can help speed up default sort on nearly-sorted, bad-pivot pick inputs where Numba quicksort lags. eg.

>>> @jit
... def sort_default(a):
...     a.sort()
...     
>>> a = np.arange(100_000)
>>> a[len(a) // 2] = -1
>>> 
>>> from timeit import timeit
>>> timeit(lambda: a.copy().sort(), number=20) / 20
0.003363966662436724
>>> timeit(lambda: sort_default(a.copy()), number=20) / 20
0.1712677250150591
>>> timeit(lambda: sort_default(a.copy()), number=20) / 20
0.16626458750106393