Chapter II Practice Report

Source: Internet
Author: User

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

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.