Import Java.lang.reflect.array;import java.nio.buffer;import Java.util.arrays;import java.util.Random;//========== =======================================//File name:binary_search//--------------------------------------------- ---------------------------------//author:common//class name: binarysearch_find//attribute://Method: Class Binarysearch_find{private int[] temp;private int searchkey;private int lowerbound = 0;//nether private int upperbound;//upper bound private int Curnum;public int [] Gettemp () {return temp;} public void Settemp (int[] temp) {this.temp = temp;} Public Binarysearch_find (int[] temp) {//Constructor this.temp = Temp;this.upperbound = temp.length-1;} public int find (int searchkey) {This.searchkey = Searchkey;while (true) {Curnum = (lowerbound+upperbound)/2;if (temp[ Curnum]==this.searchkey) {return curnum;//find}else if (lowerbound>upperbound) {return-1;//no find}else{if (temp[ Curnum]<this.searchkey) {lowerbound = curnum+1;} Else{upperbound = CurNum-1;}}}} Class Name: randomarrays//property://Method: Class Randomarrays{//generates a random array with Num public int[] getarrays (int Num) {int[] Arrays = new Int[num]; Random r = new Random (), for (int i=0;i<num;i++) {Arrays[i] = R.nextint (+),//system.out.print (arrays[i]+ ",");} return Arrays;}} Class Name: orderedarrays//property://Method: Class orderedarrays{//generates an ordered array, starting from 0 to Numpublic int[] getarrays (int Num) {int[] Arrays = new int[num];for (int i=0;i<num;i++) {Arrays[i] = I;//system.out.print (arrays[i]+ ",");} return Arrays;}} Main class//function:binary_searchpublic class Binary_search {public static void main (string[] args) {//TODO auto-generated method stub Randomarrays Array_demo = new Randomarrays ();//binarysearch_find arrays = new Binarysearch_find (array_demo.getarrays (+)); O Rderedarrays Array_demo = new Orderedarrays (); Binarysearch_find arrays = new Binarysearch_find (array_demo.getarrays (100)); System.out.println (Arrays.tostring (Arrays.gettemp ())); System.out.println (Arrays.find (1000));}}
Java data Structure--two-point lookup