Lars Hagen describes in his blog post a strategy to solve the problem of a fast incremental sort.
by Lars Hagen
From the article
I recently came across the need for an incremental sorting algorithm, and started to wonder how to do it optimally.
The incremental sorting problem is described here, and is an “online” version of partial sort. That is, if you have already sorted kk elements, you should be able to quickly sort k+1k+1 elements, and so on.
Incremental sorts can be useful for a number of cases:
- You want sorted items, but you don’t know how many elements you’ll need. This could often happen when you are filtering the resulting sequence, and you don’t know how many items will be filtered out.
- You are streaming the sequence, so even though you want the whole sequence, you want the first elements as quickly as possible.
We’ll see how branch misprediction and other constant factors can make the naive asymptotically optimal version far slower than a practical implementation.