Skip to content
Advertisement

What’s wrong with my In Place Heap Sorting code

I’m working on a school assignment and the assignment was to make a heap sorting (In Place) program. Now the program works perfectly fine for arrays with under +- 20 elements, above that it occasionally messes up, however I can’t seem to find what’s wrong.

JavaScript

The skeleton for the methods was already there so if you’re asking why certain parameters are even there, it’s because it was compulsory.

Advertisement

Answer

The problem seem’s to be indexing, the indexing for left and right seems to be wrong

final int left = root * 2 + 1; final int right = root * 2 + 2;

here you should change the code to

final int left = root * 2; final int right = root * 2 + 1;

Also remember that you have to index the array from 1, instead of 0.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement