---
title: "各种缺陷的二分法算法"
author: "holoblog"
date: "2007-12-04T20:21:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-11-11T11:23:36Z"
id: 1252042
url: "https://holoblog.iteye.com/blog/1252042"
---

二分法是思想精髓就是如果左下标和右下标得到的中间下标所在的值等于所要查找的值，算法结束；如果中间的值小于目标值，则说明目标值可能在中间值和右下标所在的区间内，就将中间下标当成左下标，继续搜索。如果中间值大于目标值，则把中间下标当成右下标，继续搜索。

这是近期在网上用google搜索“二分法算法”找到的二分法算法。结果发现各种各样的缺陷。

public int erfen()  
//二分法查找  
{  
int iindex=0; //相当于指针的东西  
int istart=0; //  
int iend=iarray.length-1;  
while(true)  
{  
icount++;  
iindex = (istart+iend)/2;  
if(iarray\[iindex]&lt;iseek)  
{  
istart = iindex;  
}  
else if(iarray\[iindex]&gt;iseek)  
{  
iend = iindex;  
}  
else  
{  
break;  
}  
}  
return icount;  
}  
用一个测试用例就能证明的这个算法错误。

0 1 2 3 4 5查找5.结果你会发现死循环。

很多人认为只要起始值的左值和右值相加为n-1就可以了。其实不然，二分法是10年多的时间才找到了一种正确的算法。如果你开始的左值设为0并且右值设为n-1,程序很大的程度上会错。除非你解决了最后的死循环问题。

下面的算法

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)left = - 1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) right = length;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) while (left &lt;= right)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) mid = (left + right) / 2 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (a\[mid] &gt; value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 中间值比目标值大  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) right = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else if (a\[mid] &lt; value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 中间值比目标值小  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) left = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 找到，中间值等于目标值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return left;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) return - 1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

可以解决上面的0 1 2 3 4 5 查找5找不到的问题。但是，却在查找不存在的数上出了问题。比如6

下面的代码解决了这个问题：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)left = - 1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) right = length;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) while (left + 1 != right) // 注意这句话！！！  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) mid = (left + right) / 2 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (a\[mid] &gt; value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 中间值比目标值大  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) right = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else if (a\[mid] &lt; value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 中间值比目标值小  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) left = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 找到，中间值等于目标值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return left;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) return - 1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

但是上的这段真的解决了所有的问题？如果left + right 超过了整数范围？

改为下面的代码：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)int BinarySearch( int value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) int left = -1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) int right = length ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) int mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) while (left+1! = right)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) mid = left + (right - left) / 2 ; // 注意这里的写法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (values\[mid] == value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else if (values\[mid] &lt; value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) left = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) right = mid;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return - 1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }

这里解决了越界问题。但是这段代码真的没有问题了吗？没有人能保证。除非能用数学证明。

另外还有一种写法：但是必须特判length==0的情况,否则一定会访问越界。