Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Binary Search Using Recursion in Python

  • Isha BansalIsha Bansal
  • July 30, 2021
  • Python Programming Examples
Binary Search Using Recursion in Python

In this tutorial, we will be understanding how to implement Binary Search with the help of Recursion. I hope by now you are familiar with both Binary Search and Recursion.

To make it simpler for you we will be covering them in brief.


What is Binary Search?

Binary search is an efficient and fast algorithm for finding an element in a sorted list of elements.

It finds elements by repeatedly dividing the array in half and then compare the middle of the division to identify in which division the element could be present.

In order to implement Binary Search, we need three-pointers namely lower bound, upper bound, and a middle pointer.

The division of a subarray is defined by lower bound and upper bound whereas the mid pointer value is compared to the value of the element that needs to be located.

Read More on Binary Search here: Binary Search Algorithm in Python


What is Recursion?

Now, Binary Search can be implemented in many ways, some of them are mentioned below:

  1. Binary Search Algorithm using looping
  2. Binary Search Algorithm using Binary Search Tree

In this tutorial, we will be implementing Binary Search with the help of recursion.

When one function calls itself can be a direct or indirect call in order to solve a smaller problem of the same type of a bigger problem, the technique is known as Recursion.

Read More on Recursion here: Recursion in Python


Binary Search Implementation using Recursion

Let’s implement the binary search algorithm using recursion in Python here. I’ve added the code with comments to help you understand what each line does.

def Binary_Search(arr,n,lb,ub,X):

    # 1. List is empty
    if(n==0):
        print("ERROR!")

    # 2. If element is not found lb exceeds ub    
    elif(lb>ub):
        print("Not found!")
    
    # 3. Keep searching for the element in array
    else:
        mid = int((lb+ub)/2)
        if(arr[mid]==X):
            print(mid+1)
        elif(arr[mid]>X):
            Binary_Search(arr,n,lb,mid,X);
        elif(arr[mid]<X):
            Binary_Search(arr,n,mid+1,ub,X);

arr = [1,2,3,4,5,6,7,8,9]
n = len(arr)
X = int(input())
Binary_Search(arr,n,0,n-1,X)

Outputs

Original List is:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
Element to Search for:  90
Result: Not found!
Original List is:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
Element to Search for:  5
Result: 5

Conclusion

In this tutorial, we understood how to implement Binary Search with the help of Recursion along with some basics of Binary Search and Recursion.

Hope you liked the tutorial! Thank you for reading!

Stay tuned for more such tutorials! 😇


Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
HeatMaps in Python – How to Create Heatmaps in Python?
Previous Post HeatMaps in Python - How to Create Heatmaps in Python?
Next Post Compute a^n in Python: Different Ways to Calculate Power in Python
Compute a^n in Python: Different Ways to Calculate Power in Python

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.