Package JUnit. test; <br/>/** <br/> * binary search. You must search the sorted sequence. Assume that there is an incremental sequence, take the number and serial number of the intermediate position midindex = (beginindex + endindex)/2, <br/> * Then fold a sequence into two halves, beginindex ~ Midindex, midindex ~ Endindex. If the number of targets T is equal to s [midindex], locate. <br/> * otherwise, if T <s [midindex], go to beginindex ~ In midindex, continue to search. If T> S [midindex], then in midindex ~ Endindex <br/> * continues searching until the target data is found. <br/> * the condition for initial constraints on input parameters and avoiding invalid calculation is, T <s [beginindex] or T> S [endindex] Or beginindex> endindex <br/> * or beginindex> S. length-1 | endindex> S. length-1 | beginindex <0 | endindex <0 <br/> */<br/> public class binarysearchtest {<br/>/** <br/> * Find the index subscript returned, otherwise,-1 is returned. <br/> * @ return index <br/> */<br/> Public int binarysearch (INT dataset [], int target, int beginindex, int endindex) {<Br/> // array verification <br/> If (Dataset = NULL | dataset. length = 0) {<br/> return-1; <br/>}< br/> // beginindex, endindex verification <br/> If (beginindex> endindex | beginindex> dataset. length-1 | endindex> dataset. length-1 | beginindex <0 | endindex <0) {<br/> system. out. println ("Error Arguments! "); <Br/> return-1; <br/>}</P> <p> // invalid parameter processing <br/> If (target <dataset [beginindex] | target> dataset [endindex] | beginindex> endindex) {<br/> return-1; <br/>}< br/> int midindex = (beginindex + endindex)/2; </P> <p> // system. out. println (midindex); <br/> If (target = dataset [midindex]) {<br/> return midindex; <br/>}< br/> else if (target <dataset [midindex]) {<br/> return binarysearch (dataset, target, beginindex, midIndex-1 ); // note midIndex-1 <br/>}< br/> else {<br/> return binarysearch (dataset, target, midindex + 1, endindex ); // note midindex + 1 <br/>}</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> binarysearchtest BS = new binarysearchtest (); </P> <p> int [] DATA = new int [] {1, 3, 5, 7, 9, 12}; </P> <p> int Index = BS. binarysearch (data, 12, 0, 5); </P> <p> system. out. println ("the position of the target number in the array is:" + index); <br/>}</P> <p>