Iterative version for sort functions in heap.