Content deleted Content added
m Dating maintenance tags: {{Citation needed}} {{Clarify}} |
copyedit, rewrite pseudocode, formal reference |
||
Line 13:
It is possible to transform the list into a [[binary heap]] in Θ(''n'') time, and then traverse the heap using a modified [[breadth-first search]] algorithm that places the elements in a [[priority queue]]{{clarify|reason=a heap is a priority queue!|date=January 2014}} (instead of the ordinary queue that is normally used in a BFS), and terminate the scan after traversing exactly ''k'' elements. As the queue size remains O(''k'') throughout the traversal, it would require O(''k'' log ''k'') time to complete, leading to a time bound of O(''n'' + ''k'' log ''k'') on this algorithm.{{citation needed|date=January 2014}}
==
More efficient than any of these are specialized partial sorting algorithms based on [[mergesort]] and [[quicksort]].
'''function'''
'''if'''
'''if'''
The resulting algorithm requires an ''expected'' time of only {{math|''O''(''n'' + ''k'' log ''k'')}}, and is quite efficient in practice, especially if we substitute selection sort when
Even better is if we don't require those
'''function'''
'''if'''
'''if'''
'''if'''
The resulting algorithm requires an expected time of only {{math|''O''(''n'')}}, which is the best such an algorithm can hope for.
==Tournament Algorithm==
Line 45:
* The [[C++]] standard specifies a library function called <code>[http://en.cppreference.com/w/cpp/algorithm/partial_sort std::partial_sort]</code>.
* The [[Python (programming language)|Python]] standard library includes functions <code>[http://docs.python.org/library/heapq.html#heapq.nlargest nlargest]</code> and <code>[http://docs.python.org/library/heapq.html#heapq.nsmallest nsmallest]</code> in its <code>heapq</code> module.
==References==
{{reflist}}
== See also ==
Line 51 ⟶ 54:
== External links ==
* J.M. Chambers (1971). [http://dl.acm.org/citation.cfm?id=362602 Partial sorting]. [[Communications of the ACM|CACM]] '''14'''(5):357–358.
[[Category:Sorting algorithms]]
|