I’m programming in Java. Every 100 ms my program gets a new number.
It has a cache with contains the history of the last n = 180 numbers.
When I get a new number x I want to calculate how many numbers there are in the cache which are smaller than x.
Afterwards I want to delete the oldest number in the cache.
Every 100 ms I want to repeat the process of calculating how many smaller numbers there are and delete the oldest number.
Which algorithm should I use? I would like to optimize for making the calculation fast as it’s not the only thing that calculated on those 100 ms.
For practical reasons and reasonable values of
nyou’re best of with a ring-buffer of primitiveints (to keep track of oldest entry), and a linear scan for determining how many values are smaller thanx.In order for this to be in
O(log n)you would have to use something like Guavas TreeMultiset. Here is an outline of how it would look.On my 1.8 GHz laptop, this solution performs 1,000,000 iterations on about 13 seconds (i.e. one iteration takes about 0.013 ms, well under 100 ms).