so I have a program that performs a heap sort and I have a remove element function. I do this by taking the last element all the way on the right side and then replace the n-th element with it. To maintain the sort, I then bubble the element down to its correct place in the heap. My friend doesn't seem to think that this will work. will it?
Heapsort is using max-heap, each time, you need to switch the root node, which is the max one, with the n
th element, then put the max element into result list from right to left.
Then, bubble the element down to its proper position, and decrease the heap's size from n to n-1.
Then, the new root node of new heap is the max element among the remaining n-1 elements. Just recursively switch root element with the n-1
th element, and again put the max element to the result list, decrease heap's size by 1 until the heap's size is 0