Which data structure is most efficient to find the top 10 largest items out of 1 million items stored in file?
(A) Min heap
(B) Max heap
(D) Sorted array
Explanation: Min heap of size 10 is sufficient to find the top 10 largest items. The algorithm can be given as follows:
1. Create the min heap with first 10 items.
2. For each remaining item, check if
2.1 The item is greater than the item stored in the head of the min heap.
2.1.1 If yes, replace it with this new item. Balance the min heap.
2.1.2 If no, do nothing.
At last, the min heap will contain the top 10 largest items.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.