I genius the official free Tutorial 29: Binary search algorithm for Java lookup

Source: Internet
Author: User

The algorithm of finding binary method

Basic steps:
First step: Get the subscript in the middle of the array
The second step: the value in the middle subscript and the target value are compared, if the target value is large, it means that the value to be found is in the back half of the array
Step three: Get the middle subscript of the right half of the array again
Fourth step: Compare the obtained intermediate subscript with the target value again
The next steps and so on, so that each lookup is in the "half" data, so called binary lookup. This is why it is necessary to sort the arrays before using the binary method to find them. If you do not sort, you will not be able to determine the "half" of the target value

Example:package algorithm.binary_search;/** *  demo binary search algorithm  *  @author   Genius Alliance  -  Yukun  */public class binaryseachdemo {public static void main (String[] args )  {int[] arr = { 1, 2, 3, 4, 5, 6, 7 };//target value, Look for 6 subscript Int tagvalue = 6;int index = binaryseach (Arr, tagvalue) in the array, if (index  > -1) {System.out.println ("target value:"  + tagValue +  "subscript is:"  + index);} &NBSP;ELSE&NBSP;{SYSTEM.OUT.PRINTLN ("No target value found:"  + tagvalue);}} /** *  the method of finding binary method  */public static int binaryseach (int[] arr, int  Tagvalue) {//Look for the first subscript of the range int firstindex = 0;//look for the last subscript of the range int lastindex = arr.length  - 1;//If the first subscript is larger than the last subscript, there is no need to look again, ending the Loop while  (Firstindex <= lastindex)  {// Gets the subscript int index =  in the middle of the lookup range (Lastindex - fiRstindex)  / 2 + firstindex;if (arr[index] == tagvalue) {//If two values are equal, the index is the subscript to be found// If the target value is found, execute the return statement here Return index;}  else if (Arr[index] > tagvalue) {//If the value at the middle subscript is larger than the target value, the lookup range is on the left side of index// So the last subscript of the lookup range should be set to index-1lastindex = index - 1;}  else {//Otherwise, the lookup range is on the right side of index//So the first subscript of the lookup range should be set to index+1firstindex = index + 1;}} If the loop has not been found after execution, return -1//because the subscript of the array cannot be-1, so 1 means that no return -1 is found;}} Run Result: Target value: 6 subscript is: 5



This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius the official free Tutorial 29: Binary search algorithm for Java lookup

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.