Binary search in Java

     srcArray[] = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,1010,srcArray.length - 1,81 </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 二分查找递归实现 </span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;int</span> binSearch(<span style="color: #0000ff;"&gt;int</span> srcArray[],<span style="color: #0000ff;"&gt;int</span> start,<span style="color: #0000ff;"&gt;int</span> end,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; key) { </span><span style="color: #0000ff;"&gt;int</span> mid = (end - start) / 2 +<span style="color: #000000;"&gt; start; </span><span style="color: #0000ff;"&gt;if</span> (srcArray[mid] ==<span style="color: #000000;"&gt; key) { </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; mid; } </span><span style="color: #0000ff;"&gt;if</span> (start >=<span style="color: #000000;"&gt; end) { </span><span style="color: #0000ff;"&gt;return</span> -1<span style="color: #000000;"&gt;; } </span><span style="color: #0000ff;"&gt;else</span> <span style="color: #0000ff;"&gt;if</span> (key ><span style="color: #000000;"&gt; srcArray[mid]) { </span><span style="color: #0000ff;"&gt;return</span> binSearch(srcArray,mid + 1<span style="color: #000000;"&gt;,end,key); } </span><span style="color: #0000ff;"&gt;else</span> <span style="color: #0000ff;"&gt;if</span> (key <<span style="color: #000000;"&gt; srcArray[mid]) { </span><span style="color: #0000ff;"&gt;return</span> binSearch(srcArray,start,mid - 1<span style="color: #000000;"&gt;,key); } </span><span style="color: #0000ff;"&gt;return</span> -1<span style="color: #000000;"&gt;; } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 二分查找普通循环实现 </span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;int</span> binSearch(<span style="color: #0000ff;"&gt;int</span> srcArray[],<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; key) { </span><span style="color: #0000ff;"&gt;int</span> mid = srcArray.length / 2<span style="color: #000000;"&gt;; </span><span style="color: #0000ff;"&gt;if</span> (key ==<span style="color: #000000;"&gt; srcArray[mid]) { </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; mid; } </span><span style="color: #0000ff;"&gt;int</span> start = 0<span style="color: #000000;"&gt;; </span><span style="color: #0000ff;"&gt;int</span> end = srcArray.length - 1<span style="color: #000000;"&gt;; </span><span style="color: #0000ff;"&gt;while</span> (start <=<span style="color: #000000;"&gt; end) { mid </span>= (end - start) / 2 +<span style="color: #000000;"&gt; start; </span><span style="color: #0000ff;"&gt;if</span> (key <<span style="color: #000000;"&gt; srcArray[mid]) { end </span>= mid - 1<span style="color: #000000;"&gt;; } </span><span style="color: #0000ff;"&gt;else</span> <span style="color: #0000ff;"&gt;if</span> (key ><span style="color: #000000;"&gt; srcArray[mid]) { start </span>= mid + 1<span style="color: #000000;"&gt;; } </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; { </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; mid; } } </span><span style="color: #0000ff;"&gt;return</span> -1<span style="color: #000000;"&gt;; } }</span></pre>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>