| m n = 1 n*m = | n/2 * 2m n is even | (n-1)/2*2m + m n is odd
37 * 52 = 18 * 2*52 + 52 = 18 * 104 + 52 = 9 * 208 + 52 = 4 * 416 + 208 + 52 = 4 * 416 + 260 = 2 * 832 + 260 = 1 * 1664 + 260 = 1924
LOMUTO_PARTITION(A,l,r)
- p← A[l]
- s← l
- for i ← l+1 to r do
- if A[i] < p then
- s ← s + 1
- swap(A[s],A[i])
- swap(A[i], A[s]
- return s
A[k] ≤ p for l < k ≤ s and A[k] > p for s < k ≤ i A[k] unknown for k > i
QUICK_SELECT(A,l, r,k)
- s ← LOMUTO_PARTITION(A,0,A.size()-1)
- if s = k-1 then
- return A[s]
- elseif s > l+k-1 then
- return QUICK_SELECT(A,l,s-1,k)
- else
- return QUICK_SELECT(A,s+1,r,k)