Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

iterative binary search python

def binary_search(a, key):
	low = 0
	high = len(a) - 1
	while low < high:
		mid = (low + high) // 2
		if key == a[mid]:
			return True
		elif key < mid:
			high = mid - 1
		else:
			low = mid + 1

	return False
Comment

iterative binary search python

"""Binary Search
Iterative
"""

def bin_iter(lst, item, low=0, high=None):
    if high is None:
        high = len(lst) - 1
    while low <= high:
        mid = (low + high) // 2
        if item > lst[mid]: # To the left
            low = mid + 1
        elif item < lst[mid]: # To the right
            high = mid - 1
        else: # Found
            return mid
    return []  # Not found
Comment

PREVIOUS NEXT
Code Example
Python :: python sort() and sorted() 
Python :: python pop a element by index 
Python :: difference between == and is 
Python :: triplets in python 
Python :: classes in python 
Python :: python text recognition 
Python :: add space before and after string python 
Python :: save model with best validation loss keras 
Python :: godot remove node from group 
Python :: getch backspace pytohn 
Python :: python Cerberus 
Python :: hiw ti count the number of a certain value in python 
Python :: h2o dataframe columns drop 
Python :: django query string route 
Python :: default dictionary value 
Python :: how to install dependencies python 
Python :: python pip past 
Python :: transform dictionary keys python 
Python :: how to hello world in python 
Python :: numpy percentile 
Python :: pascal triangle 
Python :: rotate existing labels python 
Python :: embeds discord.py 
Python :: python system performance 
Python :: install pytorch on nvidia jetson nx 
Python :: how to comment python 
Python :: stackoverflow python 
Python :: newtorkx remove node 
Python :: bitwise operators in python 
Python :: supress jupyter notebook output 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =