What will be a complexity of min heap in my case?
I need to figure out 5 smallest numbers in a list.
What I do is
public List<Integer> mins(final List<Integer> list) {
final PriorityQueue<Integer> mins = new PriorityQueue<>(5, Comparator.reverseOrder());
for (final int num: list) {
if (mins.size() > 4) {
if (mins.peek() > num) {
mins.poll();
mins.add(num);
}
} else {
mins.add(num);
}
}
final List<Integer> result = new ArrayList<>(mins);
Collections.sort(result);
return result;
}
Is it correct that the complexity of the piece of code is O(nlog5) -> O(n)? In many sources, I see that the complexity of Min Heap is nlog(n). Is it correct of the complexity of Min Heap is nlog(m) where m is a size of a heap?