The T-Files


Fri, 19 Aug 2005

Sort with stop key? Anyone?

I wonder if I can just post tricky questions on my blog and expect answers. Probably not. Anyways, this one is for the algorithm gurus (I cannot even remember how quick sort works anymore, and my Knuth Bible is about eight timezones away right now):

I want to sort a rather largish amount m of items, but because I only need to first n items, I would like to terminate the algorithm once these n items have been found and not bother to sort the whole rest (because n << m).

That would be Step One. Step Two would be an algorithm that can be put in streaming mode, so that I can iterate over the items in order, without knowing how many I need in advance (because the items will go through a filter that discards some of them. Running the filter before the sort is not an option, as the filter is very heavy and should only be invoked for n' << m items).

Thanks.