-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchRecursive.py
More file actions
38 lines (28 loc) · 664 Bytes
/
binarySearchRecursive.py
File metadata and controls
38 lines (28 loc) · 664 Bytes
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
36
37
38
def binary_search(aList, ele, start, end):
mid=start + (end-start)//2
#print mid
print "------------- ", start,end,mid
if start > end:
return -1
if start == end:
return -1
if ele == aList[0]:
return 0
elif ele == aList[-1]:
return len(aList)-1
if ele == aList[mid]:
return mid
if ele < aList[mid]:
return binary_search(aList,ele,0,mid)
elif ele > aList[mid]:
return binary_search(aList,ele,mid+1,end)
# A=[]
# print binary_search(A, 1, 0, len(A))
# A=[0,1]
# print binary_search(A, 1, 0, len(A))
A=[1]
print binary_search(A, 2, 0, len(A))
A=[1]
print binary_search(A, -1, 0, len(A))
A=[1,5]
print binary_search(A, 5, 0, len(A))