Half-fold Search Algorithm

Source: Internet
Author: User

Half-fold Search Algorithm

 

Algorithm Analysis: data elements are ordered. Three variables, low, mid, and high, are used to save the sequence numbers of the beginning, center, and end of the array elements,

Compare the Key value with a [mid] to narrow down the search scope until the search is successful or fails.

Code:

1 while (low <= high) 2 {3 mid = (high + low)/2; 4 if (a [mid] = x) 5 return mid; 6 else if (a [mid]> x) 7 high = mid-1; // "mid-1" → "-" 8 else // "mid + 1" → "+" Prevent the program from entering an infinite loop
9 low = low + 1;
10}

 

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.