문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top