C language - binary search method

1. Usage scenarios

If there is a set of data and you want to query the location of a specific data in this pile of data, then you need the program to find the data that matches the target data you want to find in this set of data. , and then return to the corresponding position. If the problem is further refined and simplified, if there is a set of sequential numbers, you need to write a program to find the location of one of the numbers. After understanding the requirements, the first idea that usually comes to our mind is to write an array, then match the numbers one by one, and finally find the position of the number, return to that position, and solve the problem.

Now let's put this idea into practice and see if we can find the number. Given an array containing ten elements, which contains 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and then try to find the position corresponding to the number 7 in it, the code is as follows:

The above code uses flag as a flag number, because after successfully finding it, break jumps out of the loop and continues execution. However, if it is looking for a number that does not exist in the array, the loop execution will also end after searching all the above data. This If there is no flag number, the program cannot determine the reason for jumping out of the loop structure. Therefore, a variable flag is defined for judgment: if it is found successfully, the value of flag is 1, otherwise it is 0. The program can use the if statement to judge which situation it is and whether to output the statement in the case where it cannot be found.

We found that this goal can be achieved in the end, and we successfully found the corresponding position of the number 7 in this number: 7. (This position refers to the position of the number rather than the subscript in the array).

However, although the corresponding position can be found by matching numbers one by one, if there are 100, 1,000 or even 10,000 numbers, and then you want to find the position corresponding to a lower number, this method seems a bit "clumsy" "It's not flexible and simple enough. You have to match all the numbers one by one from beginning to end until you find it. At this time, you need to use a simpler and faster method - the binary search method, or the binary search method.

2. How to implement the binary search method

If your classmate bought a pair of sneakers one day and only told you that the shoes were within one thousand, and then asked you to guess the price of the shoes, what would you do? There may be a small number of people who come up and punch this friend, and then say, "You didn't buy the shoes for me, but you asked me to guess the price. Versailles, you **, I ******" , but most people will still choose the right approach: start guessing from the middle of 500, instead of just stupidly guessing from 1, 2, 3 to 1000 like the above method. So the idea behind the binary search method is the same.

From this set of ordered numbers, we take the median and match it with the target number we are looking for. If it hits the soul in just one shot, we have directly found the number, but even if there is a high probability that it is incorrect, we can directly kill half of it. The data. For example: If there is an array of 1-1000 and the target number is 777, then my median is 500. After comparison, it is smaller than the target number, which means that the position of the target number can only be at the median. upward, but not below 500, then all the numbers below 500 will be eliminated. This search can eliminate half of the data, and the above method can only eliminate one number at a time, and the efficiency is relatively low. There is a very obvious gap. Then after the first search, perform a second half search, and compare with 750. This time it is still smaller than the target number, but half of the data is still removed. Continue in sequence, and you can find it from a large amount of data with only a few searches. .

Now, still using the above example, we will illustrate the following binary search method:

 In the above code, a left subscript and a right subscript are first defined to define the median subscript. After each search, if the target number > the median, then the target number will only appear above the median, then the right subscript does not need to be changed at this time, and the left subscript should be the subscript of the median + 1. Add the left and right subscripts again and divide by 2 to obtain the median of the remaining data as the new median for the next search. If the target number

The diagram of the first search is as follows. After the first search, we know that k > median, so the position of k can only appear to the right of the median. At this time, our second search wants to start from the median. Start looking for the first number on the right, so the left subscript should be mid + 1 at this time.

 So the second search is as follows. It can be seen that k

 Then subsequent searches are as shown in the figure above, until the left and right subscripts are staggered (left > right) or equal (left = right), which is the end of the loop.

 

 3. Summary

The half search method can determine whether half the amount of data matches each time, and is much more efficient than the first method. The case of k = 7 is the case with the largest number of searches. It only requires four searches to complete. However, the first method requires seven searches. In comparison, it is enough to show the efficiency of the half search method.

However, the halved search method also requires prerequisites before it can be used, that is, the set of data to be searched must be in order, and both reverse and sequential order are acceptable. In the face of disordered data, the half search method fails. This is also a shortcoming compared to the first method. Both methods have their own strengths and must be used according to different situations.

Guess you like

Origin blog.csdn.net/m0_69438595/article/details/127674816