<p>In the vein of <em>programming questions</em>: 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?</p>

[{'answer_id': 33975, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 1, 'selected': False, 'text': '<p>If you need random insert and removal, the best way is probably a sorted array. Inserts and removals should be O(log(n)). </p>\n'}, {'answer_id': 33984, 'author': 'jjnguy', 'author_id': 2598, 'author_profile': 'https://Stackoverflow.com/users/2598', 'pm_score': 4, 'selected': True, 'text': '<p>Using a min-heap is the best way.</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Heap_&#40;data_structure&#41;" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Heap_&#40;data_structure&#41;</a></p>\n\n<p>It is tailor made for this application.</p>\n'}, {'answer_id': 33985, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 1, 'selected': False, 'text': '<p>@Harpreet<br>\nThat is not optimal.  When an object is removed, erickson will have to scan entire collection to find the new smallest.</p>\n\n<p>You want to read up on <a href="http://en.wikipedia.org/wiki/Binary_search_tree" rel="nofollow noreferrer">Binary search tree</a>\'s.  MS has a good <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336800.aspx" rel="nofollow noreferrer">site</a> to start down the path.  But you may want to get a book like <a href="https://rads.stackoverflow.com/amzn/click/com/0262032937" rel="nofollow noreferrer" rel="nofollow noreferrer">Introduction to algorithms (Cormen, Leiserson, Rivest, Stein)</a> if you want to deep dive.</p>\n'}, {'answer_id': 33992, 'author': 'pbh101', 'author_id': 1266, 'author_profile': 'https://Stackoverflow.com/users/1266', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>If you need random insert and removal,\n  the best way is probably a sorted\n  array. Inserts and removals should be\n  O(log(n)).</p>\n</blockquote>\n\n<p>Yes, but you will need to re-sort on each insert and (maybe) each deletion, which, as you stated, is O(log(n)).  </p>\n\n<p>With the solution proposed by Harpreet:</p>\n\n<ul>\n<li>you have one O(n) pass in the beginning to find the smallest element</li>\n<li>inserts are O(1) thereafter (only 1 comparison needed to the already-known smallest element)</li>\n<li>deletes will be O(n) because you will need to re-find the smallest element (keep in mind Big O notation is worst case).  You could also optimize by checking to see if the element to be deleted is the (known) smallest, and if not, just don't do any of the re-check to find the smallest element.</li>\n</ul>\n\n<p>So, it depends.  One of these algorithms will be better for an insert-heavy use case with few deletes, but the other is overall more consistent.  I think I would default to Harpreet's mechanism unless I knew that the smallest number would be removed often, because that exposes a weak point in that algorithm.</p>\n"}, {'answer_id': 33998, 'author': 'pbh101', 'author_id': 1266, 'author_profile': 'https://Stackoverflow.com/users/1266', 'pm_score': 0, 'selected': False, 'text': "<p>Harpreet:</p>\n\n<blockquote>\n  <p>the inserts into that would be linear since you have to move items for an insert.</p>\n</blockquote>\n\n<p>Doesn't that depend on the implementation of the collection?  If it acts like a linked-list, inserts would be O(1), while if it were implemented like an array it would be linear, as you stated.</p>\n"}, {'answer_id': 34005, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 0, 'selected': False, 'text': '<p>Depends on which operations you need your container to support.  A <a href="http://en.wikipedia.org/wiki/Heap_(data_structure)" rel="nofollow noreferrer">min-heap</a> is the best if you might need to remove the min element at any given time, although several operations are nontrivial (amortized log(n) time in some cases).</p>\n\n<p>However, if you only need to push/pop from the front/back, you can just use a mindeque which achieves amortized constant time for all operations (including findmin).  You can do <a href="http://scholar.google.com/scholar?q=mindeque" rel="nofollow noreferrer">a scholar.google.com search</a> to learn more about this structure.  A friend and I recently collaborated to reach a much easier-to-understand and -to-implement version of a mindeque, as well.  If this is what you\'re looking for I could post the details for you.</p>\n'}, {'answer_id': 405591, 'author': 'martinus', 'author_id': 48181, 'author_profile': 'https://Stackoverflow.com/users/48181', 'pm_score': 1, 'selected': False, 'text': '<p>For occasional removes a <a href="http://en.wikipedia.org/wiki/Fibonacci_heap" rel="nofollow noreferrer">Fibonacci Heap</a> is even faster than the min-heap. Insertion is O(1), and finding the min is also O(1). Removal is O(log(n))</p>\n'}]
['https://Stackoverflow.com/questions/33973', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3474/']