-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchRecursion.py
More file actions
35 lines (33 loc) · 1.03 KB
/
BinarySearchRecursion.py
File metadata and controls
35 lines (33 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 6 22:52:53 2021
@author: sidvs
"""
# For bianry search to apply, list must be sorted
def binary_search(l,x,start,end):
# Base case: 1 element
if start == end:
if l[start] == x: # can use end also since
return start # the two are equal
else:
return -1
else:
# Divide the array into halves
mid = int((start+end)/2)
if l[mid] == x:
return(mid)
elif l[mid] > x:
# Left half
# We now check from the beginning till one place before the mid value
return binary_search(l,x,start,mid-1)
else:
# Right half
# We now check from the beginning till one place after the mid value
return binary_search(l, x, mid+1, end)
l = [20,45,60,70,90]
x=int(input("Enter search key: "))
dex = binary_search(l, x, 0, len(l)-1)
if dex==-1:
print(x, " is not found")
else:
print(x," is found at position ",dex+1)