Experimental topics:
7-1 Two-part search
Enter n values (1<=n<=1000), n non-descending integers, and x to find, use the binary lookup algorithm to find X, the subscript (0~n-1) where x is located, and the number of comparisons. If x does not exist, output-1 and number of comparisons.
Input format:
Enter a total of three lines: the first row is the N value, the second row is n integers, and the third row is the X value.
Output format:
The subscript (0~n-1) in which the output x is located and the number of comparisons. If x does not exist, output-1 and number of comparisons.
Input Sample:
4 1 2 3 4 1
Sample output:
0 2
As far as the first question of practice report is concerned, its topic is to use the binary search algorithm to find X, the subscript (0~n-1) where x is located, and the number of comparisons. If x does not exist, output-1 and number of comparisons. At first I thought of assigning the number to an array and then returning to the subscript after comparison. A recursive function is then used until the x is compared. One obstacle here is how to Output 1 when x does not exist and output the number of comparisons, and finally set T as a global variable to be resolved. The space complexity is O (n), except that there is no additional space required for the array space and several set variables for the n number.
In my case, the process of doing the problem is the fastest time, because in the middle of this I can get acquainted with the knowledge points and the algorithm thought from the specific topic. Seeing the first question of this practical problem, I thought about the arithmetic thought of the homework which I had done before, and then tried to solve the new algorithm problem with similar thought, and finally got the correct result. So, I have been successful in solving the problem, but also successfully used their own have been a bit afraid of recursive calls. So my greatest feeling is that you pay a lot of how much you will get, each problem, every line of code, every thought is chewed up, you will not consciously improve.
Chapter II Practice Report