retagged by
849 views

2 Answers

0 votes
0 votes
In a heap, looking for an arbitrary element is O(n), thus removing an element [if given by value] is O(n) as well.

If your element is given by reference, it is however possible to remove it in O(logn) by simply 'replacing' it with the last leaf [remember a heap is implemented as a complete binary tree, so there is a last leaf, and you know exactly where it is], remove these element, and re-heapify the relevant sub heap.
0 votes
0 votes

I think you have some wording problems in this question.  
"Finding a number in heapsort" ???? Whats that?
or "Finding a number in a heap ?" or "Finding a number in a heap which is sorted" (I think you mean this one).

Searching: You are talking about any arbitrary element, It will take O(logn) to search. 
You can understand it in 2 ways.
Heap is stored in array, takes O(logn) to search in sorted array using binary search.
Or, understand it like... you may have to traverse till leaf in the tree. (Height of the tree is O(logn) you know. )

Deletion: To delete arbitrary element, you have to swap it with last element & call maxheapify/min heapify..which takes O(logn) but it does not ensure the heap will be sorted.. you need to follow the procedure of sorting from that node..as like u do heapsort.   O(n) is the answer.
You can understand it in other way like... In array .. remove & shift elements which will take O(n). 

I need to know where did I go wrong. Please give proper explanations. Thank you . :)

Related questions

0 votes
0 votes
1 answer
2
pradeepchaudhary asked Jul 8, 2018
3,303 views
You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate?1)QuickSort2)MergeSort3)HeapSort4)Selection Sort...
0 votes
0 votes
0 answers
3
Rahul Ranjan 1 asked Jun 15, 2018
670 views
You are asked to sort 15 randomly generated numbers. One should prefer - 1. Bubble Sort2. Quick Sort3. Merge Sort4. Heap Sort Please explain why others 3 sorting algorith...