Binary search in Java
•
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;">//</span><span style="color: #008000;"> 二分查找递归实现 </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> binSearch(<span style="color: #0000ff;">int</span> srcArray[],<span style="color: #0000ff;">int</span> start,<span style="color: #0000ff;">int</span> end,<span style="color: #0000ff;">int</span><span style="color: #000000;"> key) { </span><span style="color: #0000ff;">int</span> mid = (end - start) / 2 +<span style="color: #000000;"> start; </span><span style="color: #0000ff;">if</span> (srcArray[mid] ==<span style="color: #000000;"> key) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> mid; } </span><span style="color: #0000ff;">if</span> (start >=<span style="color: #000000;"> end) { </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (key ><span style="color: #000000;"> srcArray[mid]) { </span><span style="color: #0000ff;">return</span> binSearch(srcArray,mid + 1<span style="color: #000000;">,end,key); } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (key <<span style="color: #000000;"> srcArray[mid]) { </span><span style="color: #0000ff;">return</span> binSearch(srcArray,start,mid - 1<span style="color: #000000;">,key); } </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 二分查找普通循环实现 </span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> binSearch(<span style="color: #0000ff;">int</span> srcArray[],<span style="color: #0000ff;">int</span><span style="color: #000000;"> key) { </span><span style="color: #0000ff;">int</span> mid = srcArray.length / 2<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (key ==<span style="color: #000000;"> srcArray[mid]) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> mid; } </span><span style="color: #0000ff;">int</span> start = 0<span style="color: #000000;">; </span><span style="color: #0000ff;">int</span> end = srcArray.length - 1<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span> (start <=<span style="color: #000000;"> end) { mid </span>= (end - start) / 2 +<span style="color: #000000;"> start; </span><span style="color: #0000ff;">if</span> (key <<span style="color: #000000;"> srcArray[mid]) { end </span>= mid - 1<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (key ><span style="color: #000000;"> srcArray[mid]) { start </span>= mid + 1<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> mid; } } </span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">; } }</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
二维码