为什么在 SAP ABAP 中存在重复条目的情况下,使用二进制搜索 returns 读取 table 第一个条目?

Why read table with binary search returns the first entry in case of duplicate entries in SAP ABAP?

二进制搜索适用于排序的条目。根据算法,如果条目数 (n) 是偶数,则它首先搜索 n/2th 条目。如果它是 key ,则返回它,否则它检查 key 是小于还是大于 n/2 位置。如果它是 less ,那么搜索从索引 1 继续到 n/2 -1,丢弃剩下的一半。类似地重复该过程直到找到搜索到的键。 如果条目数为奇数,则中间位置为 n-1/2.

所以我的问题是是否有重复的条目,我们已经按升序对它进行了排序,例如 11122233。现在如果我们使用 key = 1 读取 table 二进制搜索(请忽略语法),然后根据对于算法,n/2 = 4。但第 4 个位置不是 1,因此搜索从 1 继续到第 4 个位置。现在,n/2 = 第二个位置 1,它是关键。所以搜索在第二个索引处停止。所以返回第二个索引。

但在 abap 中读取 table 二进制搜索返回 1 的第一个条目,即索引 1。为什么这样?请解释。

因为算法has been specified and implemented that way

When the addition BINARY SEARCH is used, if there are multiple hits (due to an incomplete search key or duplicate entries in the table), the first hit according to the order of the rows in the primary index is returned. This is the row with the lowest row number.

其背后的基本原理是您更喜欢一种语言表现出 stable 行为 - 在 table 末尾添加一些完全不相关的条目不应改变 [=10] =]语句定位。