I like to program in JS and naturally there isn't a quque/prority queue implementation. But I have a thought about the relative efficiency of both.
Let's just say that I implement:
Now instead of using the prority queue my idea is just to have a class that holds an insitatiation of queue. When you pop the next in the queue just sort it using something like merge sort which is O(nlogn).
So basically now my queue implementation sorts every single time and has the same time complexity as the prority queue heap implementation, but also has less space complexity. The code is also simpler.
What am I missing here? Are there benwfits to implementing a prority queue as a heap?
class MockProrityQueue {
constructor(compareCB){
this.list = new Queue;
this.compareCB;
}
offer(item){
this.list.push(item);
}
poll(item){
this.list.sort(this.compareCB);
return this.list.pop();
}
isEmpty(){
return this.list.length === 0;
}
}