From e572354976847d3cf4c5915bd2433402730dd935 Mon Sep 17 00:00:00 2001 From: gkgaurav31 Date: Thu, 23 Jun 2022 11:57:21 +0530 Subject: [PATCH] Fix off-by-one mistake in MinHeap.java (#3162) --- .../java/com/thealgorithms/datastructures/heaps/MinHeap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java index 37434d207..b25796154 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java @@ -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); }