I am looking for an efficient data structure to represent a priority list. Specifically I need to assign a priority to a set of items and return only the top scoring items. I have looked into priority queues which operate on heaps, but they don't seem to really suit my needs. They will reorganize the heap structure as soon as I will poll the top rating item from the queue.
The simplest solution would of course be a linked list, which in the worst case would take quite long for the insertion operation.
Does anyone have a better solution?
Heaps seem very suitable, and it seems like you are going about it wrongly.
Say you wanted the top x elements (how does this x compare to n, btw?)
What you are doing is putting all into a max-heap and getting the top x.
I suggest instead, you use a min-heap of exactly x elements.
First x elements you insert into heap.
Next incoming element, you compare against the min which can be done very quickly (O(1) time) in the heap. If smaller, you just ignore the incoming element.
If incoming element is larger than min, then you increase the min to the incoming element and sift it down in the heap. This should be logx time at worst.
Once done (in nlogx time), you can retrieve the elements from the heap in sorted order in O(xlogx) time.
Depending on how your data is (and how small x is), using this min-heap solution can be really fast.
If you really really want the inserts to be super-fast and don't care much about the retrieval then you can also do the following.
Insert the elements into a vector (array with amortized O(1) insert time) in the order they come.
The use the Selection algorithm to find the xth largest element (in O(n) time, but the constants might be big). Say that number is S.
Now walk the array comparing each element with S and select the ones as large as S.
If x is reasonably sized and comparable to n (like n/2 or something) this might work out fine, but if x is small compared to n, I would suggest go with the min-heap.