Quickselect For Kth Smallest Element

Find the k-th smallest value in average linear time with randomized quickselect, zero-based ranks and a non-mutating input copy.

JavascriptData Integrity

Code

javascriptsnippet
40 lines
function swap(values, left, right) {
  const temporary = values[left];
  values[left] = values[right];
  values[right] = temporary;
}

export function quickselect(values, k, options = {}) {
  if (!Array.isArray(values)) throw new TypeError("values must be an array");
  if (!Number.isInteger(k) || k < 0 || k >= values.length) {
    throw new RangeError("k must be an integer within the array bounds");
  }
  const random = options.random ?? Math.random;
  if (typeof random !== "function") throw new TypeError("random must be a function");

  const data = values.slice();
  let left = 0;
  let right = data.length - 1;

  while (left < right) {
    const sample = random();
    if (!(sample >= 0 && sample < 1)) {
      throw new RangeError("random must return a value in [0, 1)");
    }
    const pivotIndex = left + Math.floor(sample * (right - left + 1));
    swap(data, pivotIndex, right);
    const pivot = data[right];
    let store = left;
    for (let index = left; index < right; index += 1) {
      if (data[index] < pivot) {
        swap(data, store, index);
        store += 1;
      }
    }
    swap(data, store, right);
    if (store === k) return data[store];
    if (store < k) left = store + 1;
    else right = store - 1;
  }
  return data[left];
}

How it works

Count the functions swap and quickselect as the entire surface area of Quickselect For Kth Smallest Element.

Iteration is nested: while (left < right) frames the pass, for (let index = left; index < right; index += 1) does the per-item step. The failure path is explicit — TypeError and RangeError is raised when the guard condition holds.

What to watch for

  • The guard typeof random !== "function" short-circuits the flow with an early throw when its condition holds.

Using it

The main signature reads export function quickselect(values, k, options = {})

The surface worth importing is quickselect; the helpers around it are private to the snippet.

Key statements

throw new RangeError("random must return a value in [0, 1)");
if (typeof random !== "function") throw new TypeError("random must be a function");
if (!Array.isArray(values)) throw new TypeError("values must be an array");

Interactive Tools for This Topic

FAQ

How does the code behave when input is bad?
`TypeError` and `RangeError` is raised at the guard (`typeof random !== "function"`), so invalid state never reaches the main logic.
Which packages does this snippet need?
None. Every symbol the code uses resolves inside the standard library. The file defines `swap`, `quickselect` at load time.