Question

Is it possible to use insertion sort in a heapsort to replace its swap or exchange method?

Typically swap requres 3 steps at minimum:

temp = a
a = b
b = temp

A friend of mine said that it's possible to use insertion sort to reduce the swap to one operation instead of three. Is it?

Was it helpful?

Solution

I think what he means might be that when you sift down (or up) an element in a heap, you don't store that element until you find where it's supposed to go. As such, there is only one operation per swap: storing the smaller element in its new position in the heap. The process is akin to the process in an insertion sort when you move an element to its sorted place at the beginning of the array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top