tags:

views:

51

answers:

2

I would like to know if there is any api available for Java which is just like heapq in Python.

+1  A: 

Have you looked at java.util.PriorityQueue?

Jon Skeet
Actually i wanted to do this in java.http://stackoverflow.com/questions/1034846/finding-nth-item-of-unsorted-list-without-sorting-the-list/1036240#1036240
Emil
@Emil: Okay, so what's stopping you? It's probably easiest to specify a reversing comparator, and then just use the iterator from the head of the queue.
Jon Skeet
@Jon:Is this the best way to find the k'th smallest element?I found selection algorithms with complexity O(n) as the answer to the question.But couldn't find any implementation in Java.
Emil
@Emil: No, I doubt that it's the best way to find the kth smallest element, as it's basically sorting the whole thing. Just take the selection algorithms you've found and port them to Java - it shouldn't be too hard.
Jon Skeet
A: 

Yes +1 to Jon, you know we have to :).

By the way, here is the link for elaboration.

Adeel Ansari