added the optimization for heap-sort