The return value problem for the BinarySearch () method in the arrays class in Java

Source: Internet
Author: User

Recently in the review of Java knowledge, found that it is not often used to forget very quickly ...

See the use of the BinarySearch () method, found that the book is a bit wrong, so he went to the computer experiment, and finally summed up the return value of the method.

Summary: The return value of the BinarySearch () method is: 1, if a keyword is found, the return value is the position index of the keyword in the array, and the index starts at 0, and if no keyword is found, the return value is a negative insertion point value. The so-called insertion point value is the position index of the first element larger than the keyword in the array, and the position index starts at 1.

Note: Call the Sort method before calling the BinarySearch () method to sort the array, otherwise the resulting return value is indeterminate, then the binary search algorithm is determined.

Let's look at a program instance

Package Main_package;import Java.util.arrays;public class Test {public static void main (string[] args) {int[]b=new int[]{ 4,25,10,95,06,21}; System.out.println ("Original array is:"), for (int dim1:b) {System.out.print ("" +dim1+ "");} Arrays.sort (b); System.out.println ("sorted as:"), for (int x:b) {System.out.print (x+ "");} SYSTEM.OUT.PRINTLN (); int Index=arrays.binarysearch (b, 2); System.out.println ("The return value of keyword 2 is:" +index); Index=arrays.binarysearch (b, 20); System.out.println ("The return value of keyword 20 is:" +index);    Index=arrays.binarysearch (b, 30); System.out.println ("The return value of keyword 30 is:" +index); Index=arrays.binarysearch (b, 100); System.out.println ("The return value of keyword 100 is:" +index); Index=arrays.binarysearch (b, 10); System.out.println ("The return value of keyword 10 is:" +index);}}

  

The program results are:

You can see that the keyword 2 is not in the array, and 2 is smaller than any of the elements in the array, so the value of its insertion point should be the position of element 4, which is 1 ( no keyword found starting from 1 )

The keyword 20 is also not in the array, the first number in the array is 21, so the insertion point value of 20 is 4 (no keyword is found from the index starting from 1 )

The keyword 100 is also not in the array, and 100 is larger than all elements in the array, where the insertion point value is length+1 7 ( no keyword index is found starting from 1 )

The keyword 10 is in the array, so return its index in the array to 2 ( find the keyword index starting with 0 )

The index is calculated from 1 when the insertion point value is computed, because -0=0, if calculated from 0, the return value of keyword 2 and keyword 4 in the example above is the same.

The return value problem for the BinarySearch () method in the arrays class in Java

Related Article

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.