From a400f600ad8e8146717683427a2517b0fc819eb2 Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:43:51 -0400 Subject: [PATCH] Update HeapSort.java --- HeapSort.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/HeapSort.java b/HeapSort.java index 030c84b7c..ed3a07d43 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -70,10 +70,8 @@ public class HeapSort { private void heapSubtree(int rootIndex, int lastChild) { int leftIndex = rootIndex * 2 + 1; int rightIndex = rootIndex * 2 + 2; - boolean hasLeftChild = leftIndex <= lastChild; - boolean hasRightChild = rightIndex <= lastChild; int root = this.heap[rootIndex]; - if (hasRightChild) { + if (rightIndex <= lastChild) { // if has right and left children int left = this.heap[leftIndex]; int right = this.heap[rightIndex]; if (left < right && left < root) { @@ -83,7 +81,7 @@ public class HeapSort { this.swap(rightIndex, rootIndex); this.heapSubtree(rightIndex, lastChild); } - } else if (hasLeftChild) { // if no right, but has left + } else if (leftIndex <= lastChild) { // if no right child, but has left child int left = this.heap[leftIndex]; if (left < root) { this.swap(leftIndex, rootIndex);