Details
-
Task
-
Status: Closed
-
Resolution: Fixed
-
None
-
None
-
None
Description
In the current implementation offer(E e) of TopNSort.java
@SuppressWarnings("unchecked")
public boolean offer(E e)
{
if (q.size() <= qbound) {
return q.offer(e);
}
boolean ret = true;
boolean insert;
Comparable<? super E> head = (Comparable<? super E>)q.peek();
if (ascending) { // means head is the lowest value due to inversion
insert = head.compareTo(e) <= 0; // e >= head
}
else { // means head is the highest value due to inversion
insert = head.compareTo(e) >= 0; // head is <= e
}
if (insert && q.offer(e)) {
ret = true;
q.poll();
}
return ret;
}
In the 1st line the check should be made only for "<" not "<=" i.e. it should as following
if (q.size() < qbound) {
return q.offer(e);
}
For e.g. if you set qbound as 2, with current implementation you end up adding 3 elements in the queue