900 views
3 votes
3 votes
Which data structure is most efficient to find the top 10 largest items out of 1 million items stored in file? The answer given is Min-heap anybody please explain?

1 Answer

0 votes
0 votes
Suppose sorted file contains following numbers

0,0,0,0 (many times) , 1,1,1,1.... (many times), 2,2,2,2(many times).... 100,100,100,100 (many times)

Now if we want to find top 10 largest numbers then obviously it lies on last 10 indexes of sorted list.

So by sorted list, we can get the answer in O(1) time.

In Min heap, following operations need to be performed

1) Build min heap of size 10 ---> O(10) - constant

2) n-10 comparisons and each time heapify algorithm in the worst case. --

n-10 comparisons + n-10 times heapify algorithms

O(n-10) + O(n-1*log 10) = O(n)+O(n*log 10) = O(n)

Related questions

0 votes
0 votes
1 answer
2
kickassakash asked Jul 4, 2023
420 views
I have specific doubt on this question and I’ve tried to explain that in the picture ,If anyone can explain it then it’ll be of great help. according to sachin sir th...
0 votes
0 votes
1 answer
3
Souvik33 asked Apr 2, 2023
428 views
There are Insert and Retrieve_Max operations on a set {}. for n such operations what is the time complexity of the efficient algorithm possible?$n^{2}$nlogn n logn
0 votes
0 votes
1 answer
4
Souvik33 asked Nov 2, 2022
864 views
Which data structure would be most appropriate to implement a collection of values with the following 3 characteristicsSingly link list with head and tail pointerDoubly l...