Skip to content

数据二等分

¥Bisecting data

二分法或二分查找可以在已排序数组中快速找到给定值。它通常用于在保持数组排序的情况下,查找插入新值的位置。

¥Bisection, or binary search, quickly finds a given value in a sorted array. It is often used to find the position at which to insert a new value into an array while maintaining sorted order.

bisector(accessor)

示例 · 源代码 · 使用指定的访问器函数返回一个新的角平分线。

¥Examples · Source · Returns a new bisector using the specified accessor function.

js
const bisector = d3.bisector((d) => d.Date);

如果给定的访问器接受两个参数,则它将被解释为比较函数,用于将数据中的元素 d 与搜索值 x 进行比较。Use a comparator rather than an accessor if you want values to be sorted in an order different than natural order, such as in descending rather than ascending order.以上内容等同于:

¥If the given accessor takes two arguments, it is interpreted as a comparator function for comparing an element d in the data with a search value x. Use a comparator rather than an accessor if you want values to be sorted in an order different than natural order, such as in descending rather than ascending order. The above is equivalent to:

js
const bisector = d3.bisector((d, x) => d.Date - x);

平分线可用于平分已排序的对象数组(与用于平分图元的 bisect 不同)。

¥The bisector can be used to bisect sorted arrays of objects (in contrast to bisect, which is for bisecting primitives).

bisector.right(array, x, lo, hi) {#bisector_right}

js
d3.bisector((d) => d.Date).right(aapl, new Date("2014-01-02")) // 163

类似 bisectRight,但使用此平分线的访问器。上面的代码查找 Jan 之后紧接着的行的索引。2014 年 2 月 2 日,aapl 样本数据集

¥Like bisectRight, but using this bisector’s accessor. The code above finds the index of the row immediately following Jan. 2, 2014 in the aapl sample dataset.

bisector.left(array, x, lo, hi) {#bisector_left}

js
d3.bisector((d) => d.Date).left(aapl, new Date("2014-01-02")) // 162

类似 bisectLeft,但使用此平分线的访问器。上面的代码查找 Jan 所在行的索引。2014 年 2 月 2 日,aapl 样本数据集

¥Like bisectLeft, but using this bisector’s accessor. The code above finds the index of the row for Jan. 2, 2014 in the aapl sample dataset.

bisector.center(array, x, lo, hi) {#bisector_center}

js
d3.bisector((d) => d.Date).center(aapl, new Date("2013-12-31")) // 161

返回给定排序数组中与 x 最接近的值的索引。此函数期望等分线的访问器返回一个定量值,或者等分线的比较器返回一个有符号距离;否则,此方法等同于 bisector.left。参数 lo(含)和 hi(不含)可用于指定应考虑的数组子集;默认情况下,整个使用数组。

¥Returns the index of the closest value to x in the given sorted array. This expects that the bisector’s accessor returns a quantitative value, or that the bisector’s comparator returns a signed distance; otherwise, this method is equivalent to bisector.left. The arguments lo (inclusive) and hi (exclusive) may be used to specify a subset of the array which should be considered; by default the entire array is used.

bisect(array, x, lo, hi)

js
d3.bisect(aapl.map((d) => d.Date), new Date("2014-01-02")) // 163

bisectRight 的别名。

¥Alias for bisectRight.

bisectRight(array, x, lo, hi)

js
d3.bisectRight(aapl.map((d) => d.Date), new Date("2014-01-02")) // 163

类似 bisectLeft,但返回一个插入点,该插入点位于数组中任何与 x 等价的现有条目之后(右侧)。返回的插入点 i 将数组分成两半,使得左侧 array.slice(lo, i) 中 v 的所有 v <= x 都为 v,右侧 array.slice(i, hi) 中 v 的所有 v > x 都为 v。另请参阅 bisector.right

¥Like bisectLeft, but returns an insertion point which comes after (to the right of) any existing entries equivalent to x in array. The returned insertion point i partitions the array into two halves so that all v <= x for v in array.slice(lo, i) for the left side and all v > x for v in array.slice(i, hi) for the right side. See also bisector.right.

bisectLeft(array, x, lo, hi)

js
d3.bisectLeft(aapl.map((d) => d.Date), new Date("2014-01-02")) // 162

返回数组中 x 的插入点,以保持排序顺序。参数 lo 和 hi 可用于指定应考虑的数组子集;默认情况下,整个使用数组。如果 x 已存在于数组中,则插入点将位于任何现有条目之前(左侧)。假设数组已排序,则返回值可用作 array.splice 的第一个参数。返回的插入点 i 将数组分成两半,使得左侧 array.slice(lo, i) 中 v 的所有 v < x 都为 v,右侧 array.slice(i, hi) 中 v 的所有 v >= x 都为 v。另请参阅 bisector.left

¥Returns the insertion point for x in array to maintain sorted order. The arguments lo and hi may be used to specify a subset of the array which should be considered; by default the entire array is used. If x is already present in array, the insertion point will be before (to the left of) any existing entries. The return value is suitable for use as the first argument to array.splice assuming that array is already sorted. The returned insertion point i partitions the array into two halves so that all v < x for v in array.slice(lo, i) for the left side and all v >= x for v in array.slice(i, hi) for the right side. See also bisector.left.

bisectCenter(array, x, lo, hi)

js
d3.bisectCenter(aapl.map((d) => d.Date), new Date("2013-12-31")) // 161

返回给定数字数组中与 x 最接近的值的索引。参数 lo(含)和 hi(不含)可用于指定应考虑的数组子集;默认情况下,整个使用数组。另请参阅 bisector.center

¥Returns the index of the value closest to x in the given array of numbers. The arguments lo (inclusive) and hi (exclusive) may be used to specify a subset of the array which should be considered; by default the entire array is used. See also bisector.center.