import { assert } from "../../assert.js";
import { number_compare_ascending } from "../../primitives/numbers/number_compare_ascending.js";

/**
 *
 * @param {number[]} values
 * @param {number} start
 * @param {number} end
 * @returns {number}
 */
export function computeStatisticalPartialMedian(values, start, end) {
    assert.isArrayLike(values, 'values');
    assert.isNumber(start, 'start');
    assert.greaterThanOrEqual(start, 0);

    assert.isNumber(end, 'end');
    assert.lessThan(end, values.length);

    assert.lessThanOrEqual(start, end);

    const copy = values.slice();

    copy.sort(number_compare_ascending);

    const range = end - start;

    const position = start + (range >> 1);

    return copy[position];
}
