diff --git a/algorithms/searches/binary_search_recursion.py b/algorithms/searches/binary_search_recursion.py new file mode 100644 index 0000000..3f8eaa0 --- /dev/null +++ b/algorithms/searches/binary_search_recursion.py @@ -0,0 +1,27 @@ +def binary_search(l,x,start,end): + #base case: 1 element in the list + if(start==end): + if(l[start]==x): + return start + else: + return -1 + else: + #divide the array into halves + mid=(start+end)//2 + #typecasting is changing data types + if(l[mid]==x): + return mid + elif(l[mid]>x): + #first half + return binary_search(l,x,start,mid-1) + else: + #right half + return binary_search(l,x,mid+1,end) + +l=[20,45,60,60,90] +x=int(input("What element do you need to find? ")) +index=binary_search(l,x,0,len(l)-1) +if(index==-1): + print("Not found") +else: + print("Element found at : ",index+1) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy