;; ;; Quickselect with random pivot. ;; ;; Such a pivot provides O(n) worst-case *expected* time. ;; ;; One can get true O(n) time by using "median of medians" to choose ;; the pivot, but quickselect with a median of medians pivot is a ;; complicated algorithm. See ;; https://en.wikipedia.org/w/index.php?title=Median_of_medians&oldid=1082505985 ;; ;; Random pivot has the further advantage that it does not require any ;; comparisons of array elements. ;; ;; By the way, SRFI-132 specifies that vector-select! have O(n) ;; running time, and yet the reference implementation (as of 21 May ;; 2022) uses random pivot. I am pretty sure you cannot count on an ;; implementation having "true" O(n) behavior. ;; (import (scheme base)) (import (scheme case-lambda)) (import (scheme write)) (import (only (scheme process-context) exit)) (import (only (srfi 27) random-integer)) (define (vector-swap! vec i j) (let ((xi (vector-ref vec i)) (xj (vector-ref vec j))) (vector-set! vec i xj) (vector-set! vec j xi))) (define (search-right as order predicate: ") (do ((k 1 (+ k 1))) ((= k 11)) (print-kth > k example-numbers)) (newline)