Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I efficiently keep track of the smallest element in a collection?

In the vein of programming questions: suppose there's a collection of objects that can be compared to each other and sorted. What's the most efficient way to keep track of the smallest element in the collection as objects are added and the current smallest occasionally removed?

like image 812
erickson Avatar asked Jan 25 '23 03:01

erickson


1 Answers

Using a min-heap is the best way.

http://en.wikipedia.org/wiki/Heap_(data_structure)

It is tailor made for this application.

like image 196
jjnguy Avatar answered Feb 05 '23 19:02

jjnguy