Report problem solving - binary search


Problem-solving ideas: The number had been properly arranged n, roughly divided into two equal parts, to a [n / 2] is compared with the number of the sought, if x = a [n / 2] , is to find x, end cycle, if x <a [n / 2] , the search continues in the left half of the array, if x> a [n / 2] , then the search in the right half of the array, until you find x. Somewhat similar to the high school mathematics dichotomy seeking zero.

Problem-solving difficulties: (1) Select the proper determination condition for the loop, to ensure that the cycle can be carried out smoothly.
(2) high and low values change only high = mid-1 and low = mid + 1.
Sort (3) attention has been given an array of lifting situations.
(4) hing starts equal to n-1 instead of n.

Core code:

Guess you like

Origin www.cnblogs.com/liaoying/p/12051863.html