Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python binary search

def binary_search(l : list, z : int) -> int: # z = the value being searched
	front = 0
	rear = len(l) - 1
	mid = (front + rear) // 2
	while front <= rear and l[mid] != z:
    	if l[mid] > z:
        	rear = mid - 1
    	else:
        	front = mid + 1
    	mid = (front + rear) // 2
	return mid if l[mid] == z else -1
Source by docs.python.org #
 
PREVIOUS NEXT
Tagged: #python #binary #search
ADD COMMENT
Topic
Name
7+3 =