Binary Search | Python

Binary Search implementation in Python

def binary_search(arr,ele):
	"""
		binary_search works only on sorted array, also it follows divide and conquer fundamentals
	"""

	first = 0
	last = len(arr) - 1 # as indexing starts at 0
	found = False

	while first <= last and not found:

		mid = (first+last)/2 # to get mid point of array

		if arr[mid] == ele:
			found = True
		else:
			if ele < arr[mid]:
				last = mid - 1
			else:
				first = mid + 1

	return found

b = binary_search([1,2,3,4,5],2)
print b

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s