Skip to main content
QUICK REVIEW

[Paper Review] Median Filtering is Equivalent to Sorting

Jukka Suomela|arXiv (Cornell University)|Jun 6, 2014
Algorithms and Data CompressionComputer Science15 references8 citations
TL;DR

This paper establishes that median filtering is computationally equivalent to piecewise sorting: it proposes a new algorithm that computes the sliding window median by first sorting blocks of size $k$ and then applying linear-time postprocessing. The key insight is maintaining two sorted doubly-linked lists to efficiently simulate window updates via time-reversed deletion and Knuth’s dancing links, achieving performance on par with heap-based methods and superior results on partially sorted data.

ABSTRACT

This work shows that the following problems are equivalent, both in theory and in practice: - median filtering: given an $n$-element vector, compute the sliding window median with window size $k$, - piecewise sorting: given an $n$-element vector, divide it in $n/k$ blocks of length $k$ and sort each block. By prior work, median filtering is known to be at least as hard as piecewise sorting: with a single median filter operation we can sort $Θ(n/k)$ blocks of length $Θ(k)$. The present work shows that median filtering is also as easy as piecewise sorting: we can do median filtering with one piecewise sorting operation and linear-time postprocessing. In particular, median filtering can directly benefit from the vast literature on sorting algorithms---for example, adaptive sorting algorithms imply adaptive median filtering algorithms. The reduction is very efficient in practice---for random inputs the performance of the new sorting-based algorithm is on a par with the fastest heap-based algorithms, and for benign data distributions it typically outperforms prior algorithms. The key technical idea is that we can represent the sliding window with a pair of sorted doubly-linked lists: we delete items from one list and add items to the other list. Deletions are easy; additions can be done efficiently if we reverse the time twice: First we construct the full list and delete the items in the reverse order. Then we undo each deletion with Knuth's dancing links technique.

Motivation & Objective

  • To establish a theoretical and practical equivalence between median filtering and piecewise sorting.
  • To develop a new median filtering algorithm that leverages existing sorting algorithms for improved performance.
  • To demonstrate that sorting-based median filtering can match or exceed the efficiency of traditional heap-based approaches.
  • To show that the new algorithm benefits from adaptive and cache-efficient sorting techniques.
  • To validate the algorithm’s performance across diverse data distributions and hardware platforms.

Proposed method

  • The algorithm divides the input vector into $n/k$ blocks of size $k$ and sorts each block using any comparison-based sorting algorithm.
  • It maintains two sorted, doubly-linked lists $L_A$ and $L_B$ to represent the sliding window, where $L_A$ holds the current window and $L_B$ accumulates new elements.
  • To efficiently insert new elements into $L_B$, the algorithm reverses time: it first constructs the full list of new elements and deletes them in reverse order.
  • Deletions are performed using standard doubly-linked list operations, and insertions are emulated by undoing deletions using Knuth’s dancing links technique.
  • The median is computed in linear time by merging the two sorted lists in a two-pointer fashion, maintaining pointers to the middle elements.
  • Postprocessing computes the output vector in $O(n)$ time after the initial sorting phase, resulting in an overall $O(n\log k)$ time complexity.

Experimental results

Research questions

  • RQ1Is median filtering computationally equivalent to piecewise sorting in both theory and practice?
  • RQ2Can a sorting-based approach achieve performance competitive with or better than heap-based median filtering algorithms?
  • RQ3How does the proposed algorithm perform on partially sorted or structured data compared to random inputs?
  • RQ4Can existing adaptive and cache-optimized sorting algorithms be directly leveraged to improve median filtering performance?
  • RQ5What is the impact of data locality and memory access patterns on the efficiency of median filtering algorithms?

Key findings

  • The proposed sorting-based median filtering algorithm matches the $O(n\log k)$ worst-case time complexity of heap-based methods but often performs better in practice.
  • For random inputs, the new algorithm performs comparably to the fastest heap-based implementations.
  • On partially sorted or structured inputs, the sorting-based algorithm significantly outperforms heap-based methods, especially due to better cache efficiency.
  • The algorithm leverages adaptive sorting—such as timsort or other adaptive algorithms—enabling superior performance on real-world data distributions.
  • Benchmarking across 4,620 experiments showed that SortMedian consistently outperformed HeapMedian, particularly for larger window sizes.
  • The use of time-reversed deletion with dancing links enables efficient insertion into sorted lists, making the algorithm both simple and highly performant.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.