Java data Structure--two-point lookup

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.