quantile_rank.js

import numericSort from "./numeric_sort.js";
import quantileRankSorted from "./quantile_rank_sorted.js";

/**
 * 该函数返回给定值在给定数组中的分位数位置。它会在每次运行前复制并排序数组,
 * 因此如果你知道数组已经排序,应该使用 `quantileRankSorted` 代替。
 *
 * @param {Array<number>} x 输入数组
 * @returns {number} 值
 * @example
 * quantileRank([4, 3, 1, 2], 3); // => 0.75
 * quantileRank([4, 3, 2, 3, 1], 3); // => 0.7
 * quantileRank([2, 4, 1, 3], 6); // => 1
 * quantileRank([5, 3, 1, 2, 3], 4); // => 0.8
 */
function quantileRank(x, value) {
    // 克隆并排序数组
    const sortedCopy = numericSort(x);

    return quantileRankSorted(sortedCopy, value);
}

export default quantileRank;