Java data structure and algorithm search method

Source: Internet
Author: User

one or two-point lookup method

The binary search is to compare the key of the lookup with the middle key of the sub-array, if the key found is less than the middle key, the left sub-array continues to find, if it is greater than the middle key, it is found in the right sub-array, otherwise the middle key is the element to be searched.

@Test Public voidsearchduty () {integer[]t=Newinteger[]{1,2,3,4,5,6};//Original ArrayInteger key=10;//Target ElementInteger Start= 0;//Start PositionInteger end=t.length-1;//End Position         while(start<=end) {Integer Index= (start+end)/2; if(key==T[index]) {System.out.println ("Coordinates [" +index+ "]"); return; }Else if(Key <T[index]) {End=index-1; }Else{Start=index+1; }} System.out.println ("Not Found"); }

Note: The precondition for binary lookup (binary lookup) is that ordered table sequential storage is required, and for static lookup tables, once the sorting is no longer changed, binary lookup can get good efficiency. However, for datasets that require frequent insert or delete operations, maintaining an ordered ordering can create a lot of work, and it is not recommended to use

Java data structure and algorithm search method

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.