From 6f517552561d0f4197b98d8eac24066252b4e46b Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:42:37 -0400 Subject: [PATCH] Update HeapSort.java --- HeapSort.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/HeapSort.java b/HeapSort.java index 5da774e7e..030c84b7c 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -68,25 +68,25 @@ public class HeapSort { * index of last child */ private void heapSubtree(int rootIndex, int lastChild) { - int leftIndex = rootIndex * 2 + 1; // calculate index of left children + 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) { - int left = this.heap[leftIndex]; // get left child - int right = this.heap[rightIndex]; // get right child + int left = this.heap[leftIndex]; + int right = this.heap[rightIndex]; if (left < right && left < root) { - this.swap(leftIndex, rootIndex); //swap left with root + this.swap(leftIndex, rootIndex); this.heapSubtree(leftIndex, lastChild); } else if (right < root) { - this.swap(rightIndex, rootIndex); //swap right with root + this.swap(rightIndex, rootIndex); this.heapSubtree(rightIndex, lastChild); } } else if (hasLeftChild) { // if no right, but has left int left = this.heap[leftIndex]; if (left < root) { - this.swap(leftIndex, rootIndex); //swap left and root + this.swap(leftIndex, rootIndex); this.heapSubtree(leftIndex, lastChild); } } @@ -99,8 +99,8 @@ public class HeapSort { * index of root of heap */ private void makeMinHeap(int root) { - int leftIndex = root * 2 + 1; // calculate index of left child - int rightIndex = root * 2 + 2; // calculate index of left child + int leftIndex = root * 2 + 1; + int rightIndex = root * 2 + 2; boolean hasLeftChild = leftIndex < this.heap.length; boolean hasRightChild = rightIndex < this.heap.length; if (hasRightChild) { //if has left and right