This is called finding the k-th order statistic. There's a very simple randomized algorithm (called quickselect) taking O(n)
average time, O(n^2)
worst case time, and a pretty complicated non-randomized algorithm (called introselect) taking O(n)
worst case time. There's some info on Wikipedia, but it's not very good.
Everything you need is in these powerpoint slides. Just to extract the basic algorithm of the O(n)
worst-case algorithm (introselect):
Select(A,n,i):
Divide input into ?n/5? groups of size 5.
/* Partition on median-of-medians */
medians = array of each group’s median.
pivot = Select(medians, ?n/5?, ?n/10?)
Left Array L and Right Array G = partition(A, pivot)
/* Find ith element in L, pivot, or G */
k = |L| + 1
If i = k, return pivot
If i < k, return Select(L, k-1, i)
If i > k, return Select(G, n-k, i-k)
It's also very nicely detailed in the Introduction to Algorithms book by Cormen et al.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…