Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

the most effective search methods in python with example

def BinarySearch(lys, val):
    first = 0
    last = len(lys)-1
    index = -1
    while (first <= last) and (index == -1):
        mid = (first+last)//2
        if lys[mid] == val:
            index = mid
        else:
            if val<lys[mid]:
                last = mid -1
            else:
                first = mid +1
    return index
If we use the function to compute:

>>> BinarySearch([10,20,30,40,50], 20)
Source by stackabuse.com #
 
PREVIOUS NEXT
Tagged: #effective #search #methods #python
ADD COMMENT
Topic
Name
1+1 =