Suppose I have a time series of values $v_1, \dotsc, v_n, \dots,$ (think of the data as being generated by repeated function calls, like a Python generator, or being captured in real time) and I want to maintain a window of the last $k$ values in sorted order. If $k$ were small, I could just keep re-sorting, but assume that $k$ is large. If I were doing this in C (or some such), this would be easy: I would find where each new value goes by binary search (then delete the "stale" value), but in Mathematica nothing leaps to mind. Any suggestions? One could ask a similar question in higher dimensions (where one is asking for a dynamic voronoi triangulation or kd tree), but one thing at a time...
Asked
Active
Viewed 122 times
2
-
If you ultimately need all values in sorted order, you should sort the full data set first. – David G. Stork Mar 31 '17 at 23:57
-
1@DavidG.Stork I do not, and think of the data as being generated on the fly, so I know the past, but not the future. – Igor Rivin Mar 31 '17 at 23:58
-
I recommend you edit your question to clarify your key point about data being generated "on the fly." – David G. Stork Apr 01 '17 at 00:06
-
Have you seen this? – J. M.'s missing motivation Apr 01 '17 at 00:18
-
@J.M. I had not, thanks! It does not quite answer the question, unfortunately, since in addition to the binary search, one needs to keep the array sorted, and that is going to dominate the running time, unless the right data structure is used (in this case, a doubly linked list would do fine). – Igor Rivin Apr 01 '17 at 00:30
-
I also recommend you ask for what you really want, not for a particular way you think it could be obtained. You want to sort a dynamic set of values, whether or not you explicitly use binary search. Perhaps there's a better way to do it! – David G. Stork Apr 01 '17 at 00:31
-
1@DavidG.Stork I say this in the parenthetical comment at the end (we are trying to maintain a dynamic Voronoi triangulation (in one dimension :)) – Igor Rivin Apr 01 '17 at 00:37
-
2I think this could be implemented with a priority queue see http://mathematica.stackexchange.com/q/31968/36788 – mikado Apr 01 '17 at 16:27
-
@mikado This is very close to what I want (and knowing of the existence of priority queues is certainly a great benefit), I will try to figure out if it can be adapted to my nefarious purposes. Anyway, thanks! – Igor Rivin Apr 01 '17 at 16:47