So after step 4:
- S1 contains element smaller than p
- S2 contains several occurences of p and only p's
- S3 contains element greater than p
Therefore
- if
|S1| > k
then it contains the k-th element of S - else if
|S1| + |S2| > k
then S2 contains the k-th element of S which is therefore p - else the k-th element of S in in S3. So searching the k-th element of s is the same as searching the
(k-|S1|-|S2|)
element of S3. Therefore you restart (ie iterate) the same algorithm withS = S3
andk=k-|S1|-|S2|
.
Hope this help.