Selection algorithm: Difference between revisions

Content deleted Content added
Space complexity: the previous content of the section was complete rubbish (both factually incorrect and with obviously invalid arguments in the lower bound claim).
Line 24:
More generally, a partial selection sort yields a simple selection algorithm which takes O(''kn'') time. This is asymptotically inefficient, but can be sufficiently efficient if ''k'' is small, and is easy to implement. Concretely, we simply find the minimum value and move it to the beginning, repeating on the remaining list until we have accumulated ''k'' elements, and then return the ''k''th element. Here is partial selection sort-based algorithm:
 
'''function''' select(list[1..n], k)
'''for''' i '''from''' 1 '''to''' k
minIndex = i
minValue = list[i]
'''for''' j '''from''' i+1 '''to''' n '''do'''
'''if''' list[j] < minValue '''then'''
minIndex = j
minValue = list[j]
swap list[i] and list[minIndex]
'''return''' list[k]
 
== Partition-based selection ==