Skip to content

Commit a400f60

Browse files
authored
Update HeapSort.java
1 parent 6f51755 commit a400f60

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

HeapSort.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ private void swap(int first, int second) {
7070
private void heapSubtree(int rootIndex, int lastChild) {
7171
int leftIndex = rootIndex * 2 + 1;
7272
int rightIndex = rootIndex * 2 + 2;
73-
boolean hasLeftChild = leftIndex <= lastChild;
74-
boolean hasRightChild = rightIndex <= lastChild;
7573
int root = this.heap[rootIndex];
76-
if (hasRightChild) {
74+
if (rightIndex <= lastChild) { // if has right and left children
7775
int left = this.heap[leftIndex];
7876
int right = this.heap[rightIndex];
7977
if (left < right && left < root) {
@@ -83,7 +81,7 @@ private void heapSubtree(int rootIndex, int lastChild) {
8381
this.swap(rightIndex, rootIndex);
8482
this.heapSubtree(rightIndex, lastChild);
8583
}
86-
} else if (hasLeftChild) { // if no right, but has left
84+
} else if (leftIndex <= lastChild) { // if no right child, but has left child
8785
int left = this.heap[leftIndex];
8886
if (left < root) {
8987
this.swap(leftIndex, rootIndex);

0 commit comments

Comments
 (0)