Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Code to implement iterative Binary Search

# code to implement iterative Binary Search.
def binarySearchIterative(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1
arr = [ 4, 9, 8, 20, 80 ]
find = 80
result = binarySearchIterative(arr, 0, len(arr)-1, find)
if result != -1:
	print ("Element is present at index % d" % result)
else:
	print ("Element is not present in array")
Comment

iterative bin search

"""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 time.sleep 
Python :: use python to download youtube playlist mp3 
Python :: __add__ 
Python :: how to get all distinct substrings in a string python 
Python :: confusion matrix code 
Python :: drf serializer unique together 
Python :: access key through value python 
Python :: how to use import command in python 
Python :: django login required class based views 
Python :: use rclone on colab 
Python :: python logging change handler level 
Python :: convert math equation from string to int 
Python :: graphics.py how to make a button 
Python :: one function in numpy array 
Python :: copy module in python 
Python :: python 3 documentation 
Python :: input and print 
Python :: python if nan 
Python :: tuple methods in python 
Python :: numpy subtract 
Python :: filter function in python 
Python :: how to store something in python 
Python :: NumPy bitwise_xor Syntax 
Python :: exception logging 
Python :: how to set class attributes with kwargs python 
Python :: python sound 
Python :: Run a Flask API from CMD 
Python :: Sort for Linked Lists python 
Python :: python find string in string 
Python :: root = tk() python 3 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =