-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
71 lines (57 loc) · 1.5 KB
/
search.py
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
58
59
60
61
62
63
64
65
66
67
68
# Search x in an unsorted array A.
# Return index i, A[i]=x
# If x does not belong to A, then return -1
# Linear search
def search(A, x):
i = 0 # index
while (A[i] != x):
i += 1
if (i == len(A)): #i beyond A's size
break
if (i < len(A)):
return i
else:
return -1
# Search x in an sorted array A.
# Return index i, A[i]=x
# If x does not belong to A, then return -1
# Binary search
def binsearch(A, x):
n = len(A)
lower = 0
upper = n-1
mid = (lower+upper)/2
# if x is outside array, return -1
if ((x < A[0]) | (x>A[-1])):
return -1
# Is x == A[mid]? If not search in the left or right
while (A[mid] != x):
if (lower == upper):
break
if (x<A[mid]):
upper = mid-1
else:
lower = mid+1
mid = (lower+upper)/2
if (A[mid] == x):
return mid
else:
return -1
# Search x in an sorted array A between A[lower:upper].
# Return index i, A[i]=x
# If x does not belong to A, then return -1
# Recursive binary search
def binsearch_recursive(A,lower,upper,x):
mid = (lower+upper)/2
if ((x < A[lower]) | (x>A[upper])):
return -1
elif ((lower==upper) & (A[mid] != x)):
return -1
else:
if (A[mid] == x):
ans = mid
elif (x < A[mid]):
ans = binsearch_recursive(A,lower,mid-1,x)
else:
ans = binsearch_recursive(A,mid+1,upper,x)
return ans