Computer Science, asked by Abhisri1, 1 year ago

Please hurry it's urgent!!!
I want a python program to perform binary searching using recursion also determine complexity of the program.

Answers

Answered by laraibmukhtar55
0

# Python Program for recursive binary search.  

def binarySearch (arr, l, r, x):  

# Check base  

if r >= l:  

 mid = l + (r - l)/2

 # present at the middle itself  

 if arr[mid] == x:  

  return mid  

 

 # smaller than mid

 # if present in left subarray  

 elif arr[mid] > x:  

  return binarySearch(arr, l, mid-1, x)  

 # if present in right subarray  

 else:  

  return binarySearch(arr, mid+1, r, x)  

else:  

 # not present  

 return -1

# Test

arr = [ 2, 3, 5, 10, 20 ]  

x = 10

# Function call  

result = binarySearch(arr, 0, len(arr)-1, x)  

if result != -1:  

print "Element is present at index

else:  

print " No Element is present"

Complexity of the program:

To find the complexity of this binary search program, we use the following mathematical expressions:

1=N/2x

multiplying by2x on both sides, we get:

2x=N

applying log2 on both sides:

log2(2x)=log2(N)

x∗log2(2)=log2(N)

x∗1=log2(N)

x=log2(N)

From this, we can calculate all the case scenarios.

hope it helped...

know more:

https://brainly.com/question/13390402 Write a python program that requests...

Similar questions