Problem
Binary Search
List. index () cannot cope with large-scale data queries and needs to be solved using other methods. Here we will talk about binary search.
Ideas
In terms of search, python has the list. index () method. For example:
>>> A = [2, 4, 3] # list can be unordered or ordered>. index (4) # locate and return the position of the value in the list. 1 >>>. index (5) # If this value does not exist, the error Traceback (most recent call last) is returned: File"
", Line 1, in
ValueError: 5 is not in list
This is a basic search method in python. Although it is simple, it is not sufficient for large-scale queries because the time complexity is O (n. Binary Search is an alternative method.
The binary search object is an ordered array. Pay special attention to this. Sort the array in order first. For more information about sorting, see my articles on sorting.
Basic steps:
Starting from the intermediate element of the array, if the intermediate element is exactly the element to be searched, the search process ends. If a specific element is greater than or less than the intermediate element, search in the half where the array is greater than or less than the intermediate element, and compare it from the intermediate element as before. If the array in a step is empty, it indicates that no value can be found.
This search algorithm reduces the search range by half for each comparison. Time Complexity: O (logn)
Solution (Python)
Def binarySearch (lst, value, low, high): # low, high is the search range of lst if high <low: return-1 mid = (low + high) /2 if lst [mid]> value: return binarySearch (lst, value, low, mid-1) elif lst [mid] <value: return binarySearch (lst, value, mid + 1, high) else: return mid # You can also use a loop instead of a recursive method, as follows: def bsearch (l, value): lo, hi = 0, len (l)-1 while lo <= hi: mid = (lo + hi)/2 if l [mid] <value: lo = mid + 1 elif value <l [mid]: hi = mid-1 else: return mid return-1if _ name _ = '_ main __': l = range (50) print binarySearch (l, 10, 0, 49) print bsearch (l, 10)
For python, its powerful standard library cannot be ignored. It was found that there was a module in the standard library named bisect. The document contains the following sentence:
This module provides support for maintaining a list in sorted order without having to sort the list after each insertion. for long lists of items with expensive comparison operations, this can be an improvement over the more common approach. the module is called bisect because it uses a basic bisection algorithm to do its work. the source code may be most useful as a working example of the algorith M (the boundary conditions are already right !).
When I input this passage to Baidu translation, Baidu translation of genius gave me the result:
This module provides support for maintaining the list in order for without having to class spells the list After each insertion. Long lists of items and expensive comparison operations can improve over the more common approach year. Bisect because it is called the Basic Segmentation Algorithm of the module is used to do its work. The source code may be the most useful job example of the algorithm (the boundary condition is already right !).
This is the level of Baidu. Unfortunately, google cannot be used in your country.
You can understand your English skills. The key point of this section is to explain:
The list after the module accepts the sorting. This module also applies to long list items. Because it is implemented using the binary search method, you can view its source code if you are interested (the source code is an example of a good binary search algorithm, especially to solve Extreme Boundary Conditions .) for more information about the bisect module, see the official documentation.
The following shows a function of this module.
From bisect import * def bisectSearch (lst, x): I = bisect_left (lst, x) # bisect_left (lst, x), get the position of x in the sorted lst if I! = Len (lst) and lst [I] = x: return I raise ValueErrorif _ name __= = "_ main _": lst = sorted ([2, 5, 3, 8]) print bisectSearch (lst, 5)
Python module.