What's faster: inserting into a priority queue, or sorting retrospectively?

前端 未结 10 1455
闹比i
闹比i 2020-12-12 16:58

What\'s faster: inserting into a priority queue, or sorting retrospectively?

I am generating some items that I need to be sorted at the end. I was wondering, what is

10条回答
  •  北海茫月
    2020-12-12 17:04

    I think that the insertion is more efficient in almost all cases where you are generating the data (i.e. don't already have it in a list).

    A priority queue is not your only option for insertion as you go. As mentioned in other answers a binary tree (or related RB-tree) is equally efficient.

    I would also check how the priority queue is implemented - many are based on b-trees already but a few implementations are not very good at extracting the elements (they essentially go through the entire queue and look for the highest priority).

提交回复
热议问题