- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchRecursive.py
More file actions
Latest commit
38 lines (28 loc) · 664 Bytes
/
Copy pathbinarySearchRecursive.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
defbinary_search(aList, ele, start, end):
mid=start+ (end-start)//2
#print mid
print"------------- ", start,end,mid
ifstart>end:
return-1
ifstart==end:
return-1
ifele==aList[0]:
return0
elifele==aList[-1]:
returnlen(aList)-1
ifele==aList[mid]:
returnmid
ifele<aList[mid]:
returnbinary_search(aList,ele,0,mid)
elifele>aList[mid]:
returnbinary_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]
printbinary_search(A, 2, 0, len(A))
A=[1]
printbinary_search(A, -1, 0, len(A))
A=[1,5]
printbinary_search(A, 5, 0, len(A))