Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/pythonPublic archive

Added binary search recursion program#130

Open
prathimacode-hub wants to merge1 commit intoAllAlgorithms:master
base:master
Choose a base branch
Loading
fromprathimacode-hub:master
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletionsalgorithms/searches/binary_search_recursion.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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)

[8]ページ先頭

©2009-2025 Movatter.jp