Skip to main content
Module

x/simplestatistics/index.js>quickselect

simple statistics for node & browser javascript
Go to Latest
function quickselect
import { quickselect } from "https://deno.land/x/simplestatistics@v7.7.5/index.js";

Rearrange items in arr so that all items in [left, k] range are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].

Implements Floyd-Rivest selection algorithm https://en.wikipedia.org/wiki/Floyd-Rivest_algorithm

Examples

var arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39]; quickselect(arr, 8); // = [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]

Parameters

arr

input array

k

pivot index

left
right