Kwker

Coming from another library

If you sort with NumPy, PyTorch, the C++ or Rust standard library, Java, JavaScript, Go or .NET, the Kwker call is usually a one-word change. The results are the same; Kwker is faster. In Python you can skip even that change: see No code changes.

NumPy and PyTorch

You write With Kwker Notes
np.sort(a) kwker.sorted(a) a sorted copy
a.sort() kwker.sort(a) in place
np.argsort(a, kind="stable") kwker.argsort(a) positions as uint64
np.partition(a, k) kwker.select(a, k) in place; a[k] is the value of rank k
np.argpartition(a, k) kwker.argpartition(a, k)
torch.topk(x, k) kwker.top_k(a, k, descending=True) the values and their positions
np.searchsorted(s, v) kwker.searchsorted(s, v)
np.unique(a) kwker.unique(a) the same options: index, inverse, counts
scipy.stats.rankdata(a) kwker.rank(a) average ranks for ties, as SciPy
PythonRuns on your machine.
import numpy as np
import kwker

a = np.random.default_rng(0).random(1000)
print(np.array_equal(kwker.sorted(a), np.sort(a)))
print(np.array_equal(kwker.argsort(a), np.argsort(a, kind="stable")))
values, positions = kwker.top_k(a, 3, descending=True)
print(np.array_equal(values, np.sort(a)[::-1][:3]))
Output
True
True
True

No code changes

Two calls make the libraries you already use run on Kwker, with no other change to your program:

C++

You write With Kwker
std::sort(v.begin(), v.end()) kwker::sort(v)
std::sort(v.begin(), v.end(), std::greater<>()) kwker::sort(v, kwker::Order::descending)
std::nth_element(v.begin(), v.begin() + k, v.end()) kwker::select(v.data(), v.size(), k)
std::partial_sort(v.begin(), v.begin() + k, v.end()) kwker::partial_sort(v.data(), v.size(), k)
an index vector sorted with std::stable_sort kwker::argsort(v.data(), v.size())

kwker::sort takes a std::vector, a std::span or a pointer and a length. See the C++ reference.

Rust

You write With Kwker
v.sort_unstable() kwker::sort(&mut v)
v.sort_by(f64::total_cmp) kwker::sort(&mut v) - floats sort directly
v.sort_unstable_by(|a, b| b.cmp(a)) kwker::sort_descending(&mut v)
v.select_nth_unstable(k) kwker::select_nth(&mut v, k)
an index vector sorted with sort_by_key let idx: Vec<usize> = kwker::argsort(&v, Order::ASCENDING);

Java, JavaScript, Go and .NET

Language You write With Kwker
Java Arrays.sort(a) Kwker.sort(a)
Java the k largest Kwker.topK(a, k, Kwker.DESCENDING) (their indices)
JavaScript a.sort() on a typed array kwk.sort(a) after const kwk = require("kwker")
JavaScript the k largest kwk.topK(a, k, { descending: true })
Go slices.Sort(s) kwker.Sort(s)
Go the k largest kwker.TopK(s, k, kwker.Descending, true) (values and positions)
.NET Array.Sort(a) Sorter.Sort(a)
.NET the k largest Sorter.TopK(a, k, Order.Descending) (values and indices)

Every language has the same calls - sort, select, partial sort, argsort, top-k, search - under its own naming. The API reference has a page per language.

Notes