Kwker

Quickstart: Kwker Core

Kwker Core is the engine under every other workflow: it sorts, selects and ranks arrays, in Python and 19 more languages. Install Kwker, then sort an array, find its top values, get its order and its median. About five minutes.

Each example has a tab per language; Default code language in the menu picks yours for the whole site. Python and JavaScript examples run here in your browser (a WebAssembly build: slower than native, same results).

Install

PythonShell
pip install kwker

Python needs 3.9 or newer and NumPy. For other languages and platforms, see Installation.

Sort an array

sort sorts an array in place: the array itself changes.

import numpy as np
import kwker

prices = np.array([19.99, 4.5, 120.0, 0.99, 35.0])
kwker.sort(prices)
print(prices)
[  0.99   4.5   19.99  35.   120.  ]

Every integer and float type works the same way.

Largest first, or a sorted copy

Ask for a descending order to get the largest first. Sort a copy when you need to keep the original as it is (kwker.sorted in Python).

import numpy as np
import kwker

scores = np.array([72, 95, 88, 61, 95], dtype=np.int32)
print(kwker.sorted(scores, descending=True))
print(scores)   # unchanged
[95 95 88 72 61]
[72 95 88 61 95]

Find the top results

top_k returns the k largest (or smallest) values and their positions, without sorting the whole array.

import numpy as np
import kwker

latency_ms = np.array([12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0])
values, positions = kwker.top_k(latency_ms, 3, descending=True)
print(values)
print(positions)
[310.2 260.  250.4]
[4 6 1]

Get the order, not the sorted data

argsort returns the positions that would sort the array. Use them to put other data in the same order.

import numpy as np
import kwker

names = np.array(["Ana", "Ben", "Chen", "Dara", "Eli"])
age = np.array([34, 27, 41, 27, 30], dtype=np.uint8)

order = kwker.argsort(age)
print(order)
print(names[order])
[1 3 4 0 2]
['Ben' 'Dara' 'Eli' 'Ana' 'Chen']

The median, without a full sort

select(a, k) puts the value a full sort would put at index k into a[k], without sorting the rest. That is all a median or a percentile needs: for the median, k = n // 2.

import numpy as np
import kwker

a = np.array([7, 1, 9, 4, 3, 8, 2])
mid = len(a) // 2
kwker.select(a, mid)
print("median:", a[mid])
median: 4

Check what runs on your machine

Kwker picks the fastest code for your CPU when it starts. isa() tells you which one runs.

PythonRuns on your machine.
import kwker
print(kwker.isa())       # for example: avx512
print(kwker.version())

From a terminal, python -m kwker doctor prints a full report, and python -m kwker bench --quick times Kwker against the libraries installed on your machine.

Next steps