-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.py
More file actions
58 lines (45 loc) · 1.47 KB
/
binarysearch.py
File metadata and controls
58 lines (45 loc) · 1.47 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
PSUEDOCODE
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
"""
"""
"""
# recursive method method
def binarysearch( numarry , lower_bd, upper_bd, key):
if upper_bd >= lower_bd:
mid = (lower_bd + (upper_bd - lower_bd))//2
if (numarry[mid] == key ):
return mid
elif ( numarry[mid] > key ):
# the key item search can be found in the lower bound half of the array
# calling for recursive operation on the mid value for new upper bound
return binarysearch(numarry, lower_bd, mid-1, key)
else:
# the key item search can be found in the upper bound half of the array
return binarysearch(numarry, mid+1, upper_bd, key)
else:
return -1
thelist = [1, 4, 5, 10, 13, 20 , 15,59]
upperbound = len(thelist) - 1
result = binarysearch(thelist, 0, upperbound, 1)
if thelist != -1:
print( " Element can be found at ", result, "precisely = ", thelist[result] )
else:
print("Element is not in the array")