What is Sliding Window Algorithm? Examples?

Silvester picture Silvester · Nov 25, 2011 · Viewed 97.6k times · Source

While solving a geometry problem, I came across an approach called Sliding Window Algorithm.

Couldn't really find any study material/details on it.

What is the algorithm about?

Answer

aioobe picture aioobe · Nov 25, 2011

Generally speaking a sliding window is a sub-list that runs over an underlying collection. I.e., if you have an array like

[a b c d e f g h]

a sliding window of size 3 would run over it like

[a b c]
  [b c d]
    [c d e]
      [d e f]
        [e f g]
          [f g h]

This is useful if you for instance want to compute a running average, or if you want to create a set of all adjacent pairs etc.