Very few languages have built-in support for general selection, although many provide facilities for finding the smallest or largest element of a list. A notable exception is the [[Standard Template Library]] for [[C++]], which provides a templated <code>nth_element</code> method with a guarantee of expected linear time.{{r|skiena}}
[[Python (programming language)|Python]]'s standard library (since 2.4) includes <code>heapq.nsmallest</code> and <code>heapq.nlargest</code> subroutinesfunctions for returning the smallest or largest elements from a collection, in sorted order. Different versions of Python have used different algorithms for these subroutines. As of Python version 3.13, theThe implementation maintains a [[Binary heap|binary heap]], limited to holding <math>k</math> elements, and initialized to the first <math>k</math> elements in the collection. Then, each subsequent itemsitem of the collection may replace the largest or smallest element in the heap (respectivelyif forit <code>heapqis smaller or larger than this element.nsmallest</code>andThe algorithm's memory usage is superior to heapselect (the former only holds <codemath>heapq.nlargestk</codemath>)ifelements itin ismemory smallerat ora largertime thanwhile thisthe elementlatter requires the entire dataset into memory). Theworst-caseRunning time fordepends thison implementationdata ordering. The best case is <math>O((n - k) + k\log k)</math>,worsefor thanalready thesorted data. The worst-case is <math>O(n+k\log nk)</math> thatfor wouldreverse be achievedsorted by heapselectdata. However,In for randomaverage input sequencescases, there are likely to be few heap updates and most input elements are processed with only a single comparison. For example, extracting the 100 largest or smallest values out of 10,000,000 random inputs makes 10,009,401 comparisons on average.{{r|python}}
Since 2017, [[Matlab]] has included <code>maxk()</code> and <code>mink()</code> functions, which return the maximal (minimal) <math>k</math> values in a vector as well as their indices. The Matlab documentation does not specify which algorithm these functions use or what their running {{nowrap|time is.{{r|matlab}}}}