Heapsort logic