Algorithm entry-Binary Search Algorithm

Source: Internet
Author: User

Algorithm prerequisites:

==>> Must be sorted by keyword size

The algorithm logic is as follows:

1. Compare the median value in each de-array with the searched Value

2. if the median value is smaller than the value to be searched, select the array on the right of the median value and repeat 1 until an array element equal to the value to be searched is found or a value is returned, indicates that the value to be searched does not exist in the array.

3. if the median value is greater than the value to be searched, select the array on the left of the median value and repeat 1 until an array element equal to the value to be searched is found or a value is returned, indicates that the value to be searched does not exist in the array.

The following is my personal code implementation:

[] A = {1, 3, 4, 5, 6, 8, 9, 13, 14, 17, 21 x = 21 index = String result = index =-1? "The queried value [" + x + "] is not in the array! ":" The queried value ["+ x +"] is in the array and the subscript is: "+ dichotomy ([], (a = NullPointerException ("the array cannot be blank! "} Index = 0 last =. length-1 middle = 0 (index> = 0 & index <= print (a, index, last + 1 middle = (index + last) /2 (a [middle] = (a [middle] <index = middle + 1 (a [middle]> last = middle-1-1 print ([], index, (! = & Index> = 0 & index <= StringBuffer buffer = (I = index; I <last; I ++ buffer. append (a [I] + "" System. out. println ("the array to continue searching is:" +}

Running effect:

The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 2117 212121] in the array, and the subscript is: 10
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 2117 2121212] Not in the array!
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 219 131313] in the array, and the subscript is: 7
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 211 3 4 5 61 31] in the array, and the subscript is: 0
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 218] in the array, and the subscript is: 5

 

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.