-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsequential and binary search.py
76 lines (48 loc) · 1.78 KB
/
sequential and binary 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
69
70
71
72
73
74
75
# coding: utf-8
# In[1]:
#sequential search is the easiest
#basically it is doing a traversal on all items
def sequential_search(target,raw_list):
for i in raw_list:
if i==target:
return True
return False
# In[2]:
sequential_search(1,[4,5,1])
# In[3]:
#binary search is a lil more efficient in a way
#the trouble is that it only works on a sorted list
#when the list is large, sorting could take more time than traversal
#sequential search may be a better choice
def binary_search(target,raw_list):
left=0
#for binary search
#we create the first and the last index
#binary search starts from the middle
#if the middle value is larger, we search the lower half, vice versa
#we do the same trick on the lower half recursively
#note that right=len(raw_list)+1 can handle both odd and even number case
right=len(raw_list)+1
sorted_list=sorted(raw_list)
found=False
while left<=right and not found:
#to get the middle point of any length
#we need to get the half of the sum of first and last index
#we use (left+right)//2 to get the middle value for any length
#right=len(raw_list)+1 can handle the odd number case
i=(left+right)//2
if sorted_list[i]==target:
found=True
#if the middle value is larger than the target
#we search the lower half
#we set right=i-1
#cuz item i has already been checked
#we just need the upper limit to get the middle next value
elif sorted_list[i]>target:
right=i-1
else:
#vice versa
left=i+1
return found
# In[4]:
binary_search(1,[4,5,7,9,24])