# Sliding-window median

> Find the lower median of each full window in a numeric sequence. This private workload measures the cost of sorting each window from scratch, maintaining a sorted array, and maintaining an indexed skip list as the window slides.

Find the lower median of each full window in a numeric sequence. This private workload measures the cost of sorting each window from scratch, maintaining a sorted array, and maintaining an indexed skip list as the window slides.

## Result

> Random input at width 128: sorting each window takes 147.19 ms, a sorted array 4.04 ms, an indexed skip list 9.48 ms

## Modules

* [`knotkit/frequency-table`](/modules/frequency-table/): Maintains exact counts, prefix sums, range sums, and rank selection over a fixed integer domain
* [`knotkit/ordered-map`](/modules/ordered-map/): Stores keys in comparator order with insertion, deletion, rank, selection, ceiling, floor, and range scans in either direction, as a B-tree or an indexed skip list

## Figures

* Lower medians with window width 5 on a synthetic stream with one spike.
* Sort per window, sorted array and indexed skip list agree on 12/12 windows.
* The input peaks at 71 and the rolling median peaks at 21.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/sliding-window-median/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:sliding-window-median
```

[`problems/sliding-window-median/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/sliding-window-median/README.md) states the fixture, the seed, and the result table.
