# A comparison of the binary search algorithm with the JDK and the common binary search algorithm written by itself (Java Binary Lookup source code)

Source: Internet
Author: User

First, describe

This paper analyzes and compares the binary search algorithm of JDK and the common binary search algorithm written by itself, uses bits unsigned right shift to replace the 2 operation, and uses the method of generating random number to produce a range of random number array, and calls the sort () static method of the arrays class to sort the array of int type.

math.random () Usage: Generates a random number between [0,1] (Note that you can take 0, not 1), this random number is a double type, to return the formula for a specified range of random numbers, such as an integer between [M,n]:(int) (Math.random () * (m-n+1) +m)

Second, the source code

`<span style= "FONT-SIZE:18PX;" >package tong.yue.sort;import Java.awt.renderinghints.key;import java.util.arrays;/** *  The comparison between the binary search algorithm and the common binary search algorithm written by the JDK * @author Administrator * */public class BinarySearch {public static void main (string[] args) {//Call the Randomintegerarray () method, randomly generate an array of 25 numbers int[] Valueresult = Randomintegerarray (25);//Call the sort () static method of the arrays class, Sort the above array (binary lookup can only improve search efficiency for already sorted arrays) Arrays.sort (Valueresult); System.out.print ("The result after sorting is:"); Printarrayline (Valueresult); For example I want to find the position of this number 20 int key = 20;  Call common binary lookup algorithm int index = BinarySearch (Valueresult,key);  SYSTEM.OUT.PRINTLN ("Common binary lookup algorithm results:" +key+ "in the array subscript is:" +index);  Call the JDK's own binary lookup algorithm index = BINARYSEARCHJDK (Valueresult,key); System.out.println ("The JDK's own binary lookup algorithm results:" +key+ "in the array subscript is:" +index);}        /* * The following methods are responsible for generating an array of n random numbers, and the range of random numbers is 0-49 */public static int[] Randomintegerarray (int n)//Returns an array of integer objects consisting of n random numbers {        int[] value = new Int[n]; for (int i=0; i<value.length; i++) value[i]=new Integer ((int) (Math.random () *50);//Generates aA random number of 0-49 return value; Returns the array reference}/** * Normal binary lookup method, find the keyword to return the array subscript position of the keyword, can not find the keyword returned-1 */public static int binarysearch (int[] arr,int value) {// Binary lookup int min =0;int max = Arr.length-1;int mid = (Min+max)/2;while (arr[mid]!=value) {if (arr[mid]>value) {max = mid-1;} else if (arr[mid]<value) {min = mid+1;} if (Min>max) {return-1;} Mid = (max+min)/2;} return mid;} /* * The binary search method with the JDK, find the keyword to return the array subscript location of the keyword, the keyword is not found to return a negative number associated with the last find location */public static int binarysearchjdk (int[] Arr,int Value) {//The JDK itself comes with a binary lookup int min =0;int max = Arr.length-1;while (Min<=max) {//with unsigned Right shift one bit, which can be represented by 2int mid = (Min+max) >& Gt;>1;int Midvalue = arr[mid];if (midvalue>value) {max = mid-1;} else if (arr[mid]<value) {min = mid+1;} Else{return Mid;}} Returns a negative number that is related to the final position if not found-(min+1);} private static void Printarrayline (int[] arr) {//looping the values in the Print group, not printing 10 numbers on line wrap for (int i = 0; i < arr.length; i++) {if (i%10==0 ) {System.out.println ();} System.out.print (Arr[i] + "");} System.out.println ();}} </span>`
iii. Results of Operation

A comparison of the binary search algorithm with the JDK and the common binary search algorithm written by itself (Java Binary Lookup source code)

Related Keywords:
Related Article

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.