The time complexity to find the minimum element in a min-heap is O(1), that is the primary purpose of such a container. It was literally made to find the smallest (or largest) element in constant time. The operation that is O(logn) is insertion. As others have mentioned, pop is also...

scala,data-structures,priority-queue,min-heap

You'll have to define your own Ordering : scala> object MinOrder extends Ordering[Int] { | def compare(x:Int, y:Int) = y compare x | } defined object MinOrder Then use that when creating the heap : scala> val minHeap = scala.collection.mutable.PriorityQueue.empty(MinOrder) minHeap: scala.collection.mutable.PriorityQueue[Int] = PriorityQueue() scala> minHeap.ord res1: Ordering[Int] = [email protected]

heap,heapsort,min-heap,max-heap

Just flip the sign of all your values before you put them into the heap, and again as you take them out of the heap.

The standard Java PriorityQueue does not support update. If you need to remove items, then you have to implement your own minHeap. Call heapify when you removed some items in the heap. The implementation and explanation can be found here!...

I would guess that remove() decreases heap size by 1. So, for each iteration of your loop, you are incrementing i by 1 and decrementing heap size by 1. I would change to a while loop that runs while heapsize >0.