Find--java by dichotomy

Source: Internet
Author: User

First of all, what is the dichotomy method to find.

As the name implies, the length of the Unknown origin array is binary several times, but the data in the array to be looked up when the precondition is needed is the sorted data.

Main ideas:

For the array to be looked up Arr[low,high], where high=arr.length-1, the element des to be found

(1) Determine the middle position of the array mid= (Low+high)/2

(2) Compare the lookup value des with arr[mid], if it is equal, return to the position directly, otherwise determine the new find location, continue the binary search. The region is determined as follows: If Arr[mid]>des is known by the order of the array

arr[mid,mid+1.........,high]>des; so the new interval is Arr[low,........., mid-1]; if arr[mid]<des, the order of the same reason array is known arr[low, ... mid-1]<des, the new range is arr[mid+1,......, high]. Each time the lookup is compared to the median value, you can determine if the lookup succeeds, and the current lookup interval is reduced by half


The specific code is as follows:

Package Algorithm;public class Binarysearch{public static void Main (string[] args) {int[] src=new int[]{1,3,5,7,9}; System.out.println (BinarySearch1 (src,7)); System.out.println (BinarySearch1 (src,3,0,src.length-1));} /** * Binary Search algorithm * * @param srcarray * ordered array * @param des * Find element * @return des array subscript, not found return-1 */public static int binarySearch1 (i Nt[] Srcarray,int des) {int low=0;int high=srcarray.length-1;while (low<=high) {int mid= (Low+high)/2;if (des== Srcarray[mid]) {return mid;} else if (Des<srcarray[mid]) {high=mid-1;} else{low=mid+1;}} return-1;} /** * Binary lookup (recursive) The position of the number found in the array * @param dataset * @param data * @param beginindex * @param endIndex * @return index is searched for several subscript positions */ public static int BinarySearch1 (Int[]dataset,int data,int beginindex,int endIndex) {int mid= (beginindex+endindex)/2;if (Data <dataset[beginindex]| | data>dataset[endindex]| | Beginindex>endindex) {return-1;} if (Data<dataset[mid]) {return binarySearch1 (dataset,data,beginindex,mid-1);} else if (Data>dataset[mid]) {return binarysearCH1 (Dataset,data,mid+1,endindex);} Else{return mid;}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Find--java by dichotomy

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.