Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Code of recursive binary search

# Code recursive binary search.
def binarySearchAlgorithm(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) 
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid-1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
arr = [3, 43, 56, 96]
find = 56
result = binarySearchAlgorithm(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")
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Code #recursive #binary #search
ADD COMMENT
Topic
Name
8+7 =