(Algorithm) Three examples let you understand the dichotomy

Briefly describe the differences and advantages of dichotomy and conventional sorting

Usually we are looking for the maximum value in an ordered array, or looking for a certain number. The most common way is to traverse, such as comparing from left to right, the time complexity is O(N), and using the dichotomy method, then The time complexity can be controlled to O(logN, log(2, N is usually abbreviated as logN here)).

The first simple example to get a preliminary understanding

  • Finds a specified number in an ordered array.

(1) Conventional practice, traversing one side O(N)

(2) Use the dichotomy method: we find the middle position of the entire array, and compare it with the specified number we need to find. If the middle number is smaller than the target number, we can determine that the number is on the right side of the entire array, and then the right half Divide the array in half, find the number in the middle to compare, and continue to divide until the number is found.

Because each time a half of the remaining part is compared, the time complexity is O(logN)

second case

  • In an ordered array, find the leftmost position greater than or equal to (<=) a certain number.

 

 For example, in the ordered array above, our goal is to find the leftmost number less than or equal to 2, first divide it into two, find the middle 2, which is equal to the target number (but not sure if it is the leftmost number), we choose the left half Divide the array into two, get 1, which is smaller than the target number, indicating that the target number is on its right, we take it as the left boundary, and the previous 2 as the right boundary, continue to divide into two, equal to 2, and then find that there is only 1 on its left 1. Get the target number we are looking for. (ps: It is an important difference from the previous example, it must be divided into two to get our result)

third case

(The dichotomy is not only used in ordered arrays, it is also applicable in unordered arrays)

  • In an unordered array, it is necessary to find a local minimum number. (Local minimum means that a number is smaller than its two sides, such as [312], "1" in the array is the local minimum, and the number at both ends [132], "1" and "2" in the array are both local minimum )

When we encounter a problem, we must first look at its characteristics, whether it is ordered or not.

First check whether the numbers at both ends are the local minimum, if yes, you can return directly, if not, we divide into two, find the middle number to see if it is the local minimum, if not, it is greater than the numbers on both sides, then, whether it is left or right There must be a local minimum, see the figure below for reference.

 They must have a downward trend on the side, and if both ends are downward, there must be a local minimum in the middle.

Guess you like

Origin blog.csdn.net/qq_48860282/article/details/123719787