Fix off-by-one mistake in MinHeap.java (#3162)

This commit is contained in:
gkgaurav31 2022-06-23 11:57:21 +05:30 committed by GitHub
parent c0b2c56628
commit e572354976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,7 +50,7 @@ public class MinHeap implements Heap {
// Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) {
double key = minHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
while (getElementKey((int) Math.floor(elementIndex / 2.0) + 1) > key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2.0);
}