iterative binary search in python

Switch to Dreamhost, and we will move your web site. No kidding.

Today I am going to post the code for binary search. This one is iterative and the next one will be recursive. If you don’t know about binary search, please read the wikipedia article first.

Now, here is my Python implementation of binary search algorithm:


def binary_search_iterative(li, left, right, key):
while True:
if left > right:
return -1
mid = (left + right) / 2
if li[mid] == key:
return mid
if li[mid] > key:
right = mid - 1
else:
left = mid + 1

if...

A historical timeline of Codango™

Original Source

#codango #developer #development #coder #coding

We're happy to share this resource that we found. The content displayed on this page is property of it's original author and/or their organization.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*