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

前端 未结 10 1470
闹比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:08

    A priority queue is usually implemented as a heap. Sorting using a heap is on average slower than quicksort, except that quicksort has a worse worst case performance. Also heaps are relatively heavy data structures, so there's more overhead.

    I'd reccomend sort at end.

提交回复
热议问题