在二进制搜索中,我们会在中间汇总



当low =中间时,我们更改m = l (h-l 1(/2。

如果您是指 l+h是奇数时的情况,它将自动圆形,您不需要任何明确的代码即可绕mid。>

给定n个元素的数组A具有值或记录A0 ... AN -1的元素,排序以使A0≤...≤An -1和目标值T,以下子例程使用二进制搜索来查找索引t中的t。[7]

Set L to 0 and R to n − 1.
If L > R, the search terminates as unsuccessful.
Set m (the position of the middle element) to the floor (the largest previous integer) of (L + R) / 2.
If Am < T, set L to m + 1 and go to step 2.
If Am > T, set R to m – 1 and go to step 2.
Now Am = T, the search is done; return m.

您永远找不到m=l+(h-l+1)/2

之类的东西

相关内容

最新更新