Java Binary lookup-binary lookup algorithm

Source: Internet
Author: User

Two-point Search:


This algorithm is relatively simple and easy to understand. The algorithm is looking for an ordered array, so you want to use this algorithm

first, you want to sort the array.


There are actually three pointers, starting with the pointer, the end pointer, the middle pointer, to Start. Binary find.

The steps are as Follows:

1, determine three pointers, Start,end,middleindex.

2, the judgment start<=end, if satisfies, executes this method, does not satisfy, returns, cannot find.

3, in the premise of 2, we find its binary, middleindex = Start+end >> 1, take the middle Value.

4. Determine if the value of the middle position is equal to the target value, and return to this position if it is equal.

unequal, if the value of the middle position is larger than the target value, the array in the left half is recursively looking for the target value,

otherwise, the array on the right looks for the recursive target Value.

5. Return Value.


package com.test4;/** *  binary search  *  *  @author  sdc * */public  class BinarySearch {//  the number of times an element needs to be found static int count;public static void  main (string[] args)  {}/** *  binary lookup--recursive form, binary lookup is an ordered array lookup  *  * @ param sortarray *  @param  start *  @param  end *  @param  target  *  @return  */public static int binarysearch (int[] sortarray, int  Start, int end, int target)  {if  (sortarray == null)  {return  -1;} int sortarraylength = sortarray.length;if  (sortarraylength == 1)  {return  sortarray[0];} count++;//  really started, find if  (start <= end)  {//  Middle position int middleindex =   (start + end)  >> 1;//  Median value int middledata = sortarray[ Middleindex];if  (middledata == target)  {return middleindex;}  else if  (middledata < target)  {return binarysearch (sortarray,  middleindex + 1, end, target);}  else {return binarysearch (sortarray, start, middleindex - 1, target);}} The return value given by  else { //  is not return -1;}} /** *  binary search-non-recursive form  *  *  @param  sortArray *  @param  target  *  @return  */public static int serarch (int[] sortarray, int  Target)  {if  (sortarray == null)  {return -1;}   Start position int start = 0;//  End position int end = sortarray.length - 1; while  (start <= end)  { //  always loops to find//  middle position int middleindex =  (start + end)  >> 1;if (sortarray[middleindex] == target) &nbsP {return middleindex;} Else if (sortarray[middleindex] > target)  {end = middleindex -1;} else {start = middleindex +1;}} return -1;}}


End:

This article is from the "10093778" blog, please be sure to keep this source http://10103778.blog.51cto.com/10093778/1918333

Java Binary lookup-binary lookup algorithm

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.