Lean Algorithm: Running Median over a Fixed-Size Window of a Data Stream using Optimized Nonsorting
Authors/Creators
Contributors
Researcher:
Description
This project presents an optimized nonsorting algorithm for calculating the
running median of a fixed-size window.
Unlike sorting-based implementations, the algorithm does not sort the window
values or maintain them in sorted order. It stores the window in a circular
buffer and uses the current median as a reference for determining whether the
median must change after a new value replaces the oldest one.
The optimized version tracks duplicate values associated with the median and
uses this information to avoid some buffer scans. This can reduce the number
of operations for data streams containing repeated values.
The implementation supports integer and, with minor modifications,
floating-point data types. It is intended for testing, comparison, and
educational purposes.
## Key Features
- Optimized nonsorting algorithm; the buffer remains unsorted.
- Fixed-size sliding window implemented as a circular buffer.
- Uses the current median as a reference for each update.
- Tracks duplicate values on either side of the current median.
- Avoids sorting, shifting, or rearranging buffer elements.
- Best-case time complexity: O(1).
- Worst-case time complexity: O(N).
- Memory complexity: O(N); only one data buffer is required.
- Duplicate counters can avoid some buffer scans.
- Supports integer and floating-point data types.
- Designed for resource-constrained embedded systems.
- Includes optional performance counters for testing and analysis.
- Open-source C implementation under the MIT License.
Files
RunMedianNoSortOptimized261026.txt
Additional details
Related works
- Is variant form of
- Software: 10.5281/zenodo.23601248 (DOI)
- Software: 10.5281/zenodo.23626176 (DOI)
- Software: 10.5281/zenodo.23670710 (DOI)
Dates
- Copyrighted
-
2026-10-26First Publishing
Software
- Repository URL
- https://github.com/mmoellg/Running-Median-over-a-Fixed-Size-Window-of-a-Data-Stream
- Programming language
- C
- Development Status
- Active