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.
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
knotkit/frequency-table : Maintains exact counts, prefix sums, range sums, and rank selection over a fixed integer domain
knotkit/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
{"v":1,"box":[48.3056,94.1597,913.7333,447.8555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":0,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":0,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 3: the median peaks at 22, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[2,18,3,19,4,20,5,19,6,19,7,18,8,18,9,19,10,20,11,20,12,21,13,22,14,22,15,20,15.5,20],"p":[186.48,346.8,237.71,296.27,288.93,245.74,340.15,296.27,391.38,296.27,442.6,346.8,493.83,346.8,545.05,296.27,596.27,245.74,647.5,245.74,698.72,195.22,749.94,144.69,801.17,144.69,852.39,245.74,878,245.74],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[84.03,397.33,135.26,346.8,186.48,245.74,237.71,296.27,340.15,296.27,391.38,346.8,442.6,397.33,493.83,296.27,545.05,245.74,596.27,245.74,647.5,195.22,698.72,144.69,749.94,144.69,801.17,296.27,852.39,245.74],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
input spike 71 (off scale)
Value
Lower median
Input
Width 3: the median peaks at 22, not 71
{"v":1,"box":[48.3056,608.6597,913.7333,962.3555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":1,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":1,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 5: the median peaks at 21, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[4,19,5,19,6,19,7,19,8,19,9,19,10,19,11,20,12,20,13,21,14,21,15,21,15.5,21],"p":[288.93,810.77,340.15,810.77,391.38,810.77,442.6,810.77,493.83,810.77,545.05,810.77,596.27,810.77,647.5,760.24,698.72,760.24,749.94,709.72,801.17,709.72,852.39,709.72,878,709.72],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[84.03,911.83,135.26,861.3,186.48,760.24,237.71,810.77,340.15,810.77,391.38,861.3,442.6,911.83,493.83,810.77,545.05,760.24,596.27,760.24,647.5,709.72,698.72,659.19,749.94,659.19,801.17,810.77,852.39,760.24],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 5: the median peaks at 21, not 71
{"v":1,"box":[48.3056,1123.1597,913.7333,1476.8555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":2,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":2,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 7: the median peaks at 20, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[6,19,7,19,8,19,9,19,10,19,11,19,12,20,13,20,14,20,15,20,15.5,20],"p":[391.38,1325.27,442.6,1325.27,493.83,1325.27,545.05,1325.27,596.27,1325.27,647.5,1325.27,698.72,1274.74,749.94,1274.74,801.17,1274.74,852.39,1274.74,878,1274.74],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[84.03,1426.33,135.26,1375.8,186.48,1274.74,237.71,1325.27,340.15,1325.27,391.38,1375.8,442.6,1426.33,493.83,1325.27,545.05,1274.74,596.27,1274.74,647.5,1224.22,698.72,1173.69,749.94,1173.69,801.17,1325.27,852.39,1274.74],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 7: the median peaks at 20, not 71
{"v":1,"box":[48.3056,1637.6597,913.7333,1991.3555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":3,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":3,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 9: the median peaks at 20, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[8,19,9,19,10,19,11,19,12,20,13,20,14,20,15,20,15.5,20],"p":[493.83,1839.77,545.05,1839.77,596.27,1839.77,647.5,1839.77,698.72,1789.24,749.94,1789.24,801.17,1789.24,852.39,1789.24,878,1789.24],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[84.03,1940.83,135.26,1890.3,186.48,1789.24,237.71,1839.77,340.15,1839.77,391.38,1890.3,442.6,1940.83,493.83,1839.77,545.05,1789.24,596.27,1789.24,647.5,1738.72,698.72,1688.19,749.94,1688.19,801.17,1839.77,852.39,1789.24],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 9: the median peaks at 20, not 71
{"v":1,"box":[48.3056,94.1597,465.7333,447.8555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":0,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":0,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 3: the median peaks at 22, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[2,18,3,19,4,20,5,19,6,19,7,18,8,18,9,19,10,20,11,20,12,21,13,22,14,22,15,20,15.5,20],"p":[114.95,346.8,139.66,296.27,164.37,245.74,189.07,296.27,213.78,296.27,238.49,346.8,263.2,346.8,287.9,296.27,312.61,245.74,337.32,245.74,362.02,195.22,386.73,144.69,411.44,144.69,436.15,245.74,448.5,245.74],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[65.54,397.33,90.25,346.8,114.95,245.74,139.66,296.27,189.07,296.27,213.78,346.8,238.49,397.33,263.2,296.27,287.9,245.74,312.61,245.74,337.32,195.22,362.02,144.69,386.73,144.69,411.44,296.27,436.15,245.74],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
input spike 71 (off scale)
Value
Lower median
Input
Width 3: the median peaks at 22, not 71
{"v":1,"box":[48.3056,608.6597,465.7333,962.3555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":1,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":1,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 5: the median peaks at 21, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[4,19,5,19,6,19,7,19,8,19,9,19,10,19,11,20,12,20,13,21,14,21,15,21,15.5,21],"p":[164.37,810.77,189.07,810.77,213.78,810.77,238.49,810.77,263.2,810.77,287.9,810.77,312.61,810.77,337.32,760.24,362.02,760.24,386.73,709.72,411.44,709.72,436.15,709.72,448.5,709.72],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[65.54,911.83,90.25,861.3,114.95,760.24,139.66,810.77,189.07,810.77,213.78,861.3,238.49,911.83,263.2,810.77,287.9,760.24,312.61,760.24,337.32,709.72,362.02,659.19,386.73,659.19,411.44,810.77,436.15,760.24],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 5: the median peaks at 21, not 71
{"v":1,"box":[48.3056,1123.1597,465.7333,1476.8555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":2,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":2,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 7: the median peaks at 20, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[6,19,7,19,8,19,9,19,10,19,11,19,12,20,13,20,14,20,15,20,15.5,20],"p":[213.78,1325.27,238.49,1325.27,263.2,1325.27,287.9,1325.27,312.61,1325.27,337.32,1325.27,362.02,1274.74,386.73,1274.74,411.44,1274.74,436.15,1274.74,448.5,1274.74],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[65.54,1426.33,90.25,1375.8,114.95,1274.74,139.66,1325.27,189.07,1325.27,213.78,1375.8,238.49,1426.33,263.2,1325.27,287.9,1274.74,312.61,1274.74,337.32,1224.22,362.02,1173.69,386.73,1173.69,411.44,1325.27,436.15,1274.74],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 7: the median peaks at 20, not 71
{"v":1,"box":[48.3056,1637.6597,465.7333,1991.3555],"x":{"s":"linear","lim":[-0.6975,16.1975],"inv":false,"g":3,"t":"Sample position (window end)","tk":[[0,"0"],[5,"5"],[10,"10"],[15,"15"]]},"y":{"s":"linear","lim":[16,23],"inv":false,"g":3,"tk":[[17,"17"],[18,"18"],[19,"19"],[20,"20"],[21,"21"],[22,"22"]]},"ti":"Width 9: the median peaks at 20, not 71","a":[{"k":"line","c":"#1d5fa6","s":"x","d":[8,19,9,19,10,19,11,19,12,20,13,20,14,20,15,20,15.5,20],"p":[263.2,1839.77,287.9,1839.77,312.61,1839.77,337.32,1839.77,362.02,1789.24,386.73,1789.24,411.44,1789.24,436.15,1789.24,448.5,1789.24],"ds":"post"},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[0,17,1,18,2,20,3,19,5,19,6,18,7,17,8,19,9,20,10,20,11,21,12,22,13,22,14,19,15,20],"p":[65.54,1940.83,90.25,1890.3,114.95,1789.24,139.66,1839.77,189.07,1839.77,213.78,1890.3,238.49,1940.83,263.2,1839.77,287.9,1789.24,312.61,1789.24,337.32,1738.72,362.02,1688.19,386.73,1688.19,411.44,1839.77,436.15,1789.24],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"}],"lg":[]}
0
5
10
15
Sample position (window end)
17
18
19
20
21
22
Value
Width 9: the median peaks at 20, not 71
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 at build time, from the fixtures the benchmark uses; it reproduces the README figure.
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 states the fixture, the seed, and the result table.