22 lines
516 B
Plaintext
22 lines
516 B
Plaintext
procedure main(A)
|
|
every writes(" ",select(1 to *A, A, 1, *A)|"\n")
|
|
end
|
|
|
|
procedure select(k,A,min,max)
|
|
repeat {
|
|
pNI := partition(?(max-min)+min, A, min, max)
|
|
pD := pNI - min + 1
|
|
if pD = k then return A[pNI]
|
|
if k < pD then max := pNI-1
|
|
else (k -:= pD, min := pNI+1)
|
|
}
|
|
end
|
|
|
|
procedure partition(pivot,A,min,max)
|
|
pV := (A[max] :=: A[pivot])
|
|
sI := min
|
|
every A[i := min to max-1] <= pV do (A[sI] :=: A[i], sI +:= 1)
|
|
A[max] :=: A[sI]
|
|
return sI
|
|
end
|