Day4 recursive binary lookup and day4 recursive binary Lookup

Source: Internet
Author: User

Day4 recursive binary lookup and day4 recursive binary Lookup

There is an existing sequence, data = [for I in range (, 3)]. Now we need to check whether a number exists in the list. We know that, we can use the in or _ contains _ () method to determine whether a value is in the list, but the list is also traversed one by one to see if it is equal to a value in the list, if not, False is returned. If not, True is returned.

Def binary_search (data, find_n ):
Mid_n = int (len (data)/2)
# Recursion must have an end condition. Here, the end condition must end when there are only two lengths.
If mid_n> 1: # recursion end condition, if there are only two elements, there is no need to make another judgment.
If data [mid_n]> find_n: # if the median value is greater than the search value, it is displayed on the left side of the list.
Print (data [: mid_n])
Binary_search (data [: mid_n], find_n) # recursive again to narrow down the scope
Elif data [mid_n] <find_n: # If the median value is smaller than the search value, it is on the right of the median value.
Print (data [mid_n:])
Binary_search (data [mid_n:], find_n) # recursion to narrow down the scope
Elif data [mid_n] = find_n: # If the median value is equal to the search value, it is displayed in the list.
Print ("% s in List data" % find_n)
Elif mid_n = 1:
# We know that there are two cases when recursion ends. The first case is that the length is equal to 2, and the other case is that the length is equal to 3.
Print (data)
If len (data) = 2: # if the list length is equal to 2, you can determine whether there is a value equal to the search value,
If data [0] = find_n or data [1] = find_n:
Print ("% s in List data" % find_n)
Else:
Print ("% s not in List data" % find_n)
Else:
# When the list length is equal to 3, compare them one by one
If data [0] = find_n or data [1] = find_n or data [2] = find_n:
Print ("% s in List data" % find_n)
Else:
Print ("% s not in List data" % find_n)


If _ name _ = "_ main __":
Data = [I for I in range (, 4)]
Binary_search (data, 10)

The flowchart above describes the code running process in detail. We know that if recursion is used, there are only two situations when traversing the list, or the value to be searched is in the list, or it is not in the list, and, at the end of the list, the length of the list can only be 1 and 2. In this case, we need to consider the situation. There are three cases above, (1) in the traversal process, the median value is exactly the value we want to search for. (2) If the list length is 1, then the remaining elements are compared. In this case, the half value is less than or equal to the minimum value or greater than or equal to the maximum value; (3) The length of the list is 2. In this case, the length is equal to or greater than the maximum value and less than or equal to the minimum value. The reason for the length of List 1 and list 2 is that the length of the original data is related to that of the data, because the original data contains singular and even numbers, the results also show these two situations. The above three situations must be analyzed in detail. Otherwise, an error is certainly returned. Some people may be able to find the value in the list, but if not, an error will be reported. Or an odd number, or an even number, or an even number.

Related Article

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.