-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
binary_search.py
91 lines (73 loc) · 2.89 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Beginner Python Project - Binary Search Implementation by Kylie Ying
YouTube Kylie Ying: https://www.youtube.com/ycubed
Twitch KylieYing: https://www.twitch.tv/kylieying
Twitter @kylieyying: https://twitter.com/kylieyying
Instagram @kylieyying: https://www.instagram.com/kylieyying/
Website: https://www.kylieying.com
Github: https://www.github.com/kying18
Programmer Beast Mode Spotify playlist: https://open.spotify.com/playlist/4Akns5EUb3gzmlXIdsJkPs?si=qGc4ubKRRYmPHAJAIrCxVQ
"""
import random
import time
# Implementation of binary search algorithm!!
# We will prove that binary search is faster than naive search!
# Essence of binary search:
# If you have a sorted list and you want to search this array for something,
# You could go through each item in the list and ask, is this equal to what we're looking for?
# But we can make this *faster* by leveraging the fact that our array is sorted!
# Binary search ~ O(log(n)), naive search ~ O(n)
# In these two examples, l is a list in ascending order, and target is something that we're looking for
# Return -1 if not found
# naive search: scan entire list and ask if its equal to the target
# if yes, return the index
# if no, then return -1
def naive_search(l, target):
# example l = [1, 3, 10, 12]
for i in range(len(l)):
if l[i] == target:
return i
return -1
# binary search uses divide and conquer!
# we will leverage the fact that our list is SORTED
def binary_search(l, target, low=None, high=None):
if low is None:
low = 0
if high is None:
high = len(l) - 1
if high < low:
return -1
# example l = [1, 3, 5, 10, 12] # should return 3
midpoint = (low + high) // 2 # 2
# we'll check if l[midpoint] == target, and if not, we can find out if
# target will be to the left or right of midpoint
# we know everything to the left of midpoint is smaller than the midpoint
# and everything to the right is larger
if l[midpoint] == target:
return midpoint
elif target < l[midpoint]:
return binary_search(l, target, low, midpoint-1)
else:
# target > l[midpoint]
return binary_search(l, target, midpoint+1, high)
if __name__=='__main__':
# l = [1, 3, 5, 10, 12]
# target = 7
# print(naive_search(l, target))
# print(binary_search(l, target))
length = 10000
# build a sorted list of length 10000
sorted_list = set()
while len(sorted_list) < length:
sorted_list.add(random.randint(-3*length, 3*length))
sorted_list = sorted(list(sorted_list))
start = time.time()
for target in sorted_list:
naive_search(sorted_list, target)
end = time.time()
print("Naive search time: ", (end - start), "seconds")
start = time.time()
for target in sorted_list:
binary_search(sorted_list, target)
end = time.time()
print("Binary search time: ", (end - start), "seconds")