Skip to content

Commit 59bbb71

Browse files
author
codebasics
committed
binary search
1 parent e77d720 commit 59bbb71

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

Algorithms/BinarySearch/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### Binary Search Exercise
2+
1. When I try to find number 5 in below list using binary search, it doesn't work and returns me -1 index. Why is that?
3+
4+
```numbers = [1,4,6,9,10,5,7]```
5+
6+
1. Find index of all the occurances of a number from sorted list
7+
8+
```
9+
numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
10+
number_to_find = 15
11+
```
12+
This should return 5,6,7 as indices containing number 15 in the array
13+
14+
[Solution]()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
############ BINARY SEARCH EXERCISE SOLUTION: CODEBASICS YOUTUBE CHANNEL ####################
2+
3+
################### PROBLEM 1 #######################
4+
# When I try to find number 5 in below list using binary search, it doesn't work and returns me -1 index. Why is that?
5+
# numbers = [1,4,6,9,10,5,7]
6+
# Answer: It is because the list is not sorted! Binary search requires that list has to be sorted
7+
8+
################### PROBLEM 2 #######################
9+
# Problem: Find index of all the occurances of a number from sorted list
10+
# Solution here tries to find an index of a number using binary search. Now since the list is sorted,
11+
# it can do left and right scan from the initial index to find all occurances of a given element
12+
# This method is not most efficient and I want you to figure out even a better way of doing it. In
13+
# any case below method is still effective
14+
15+
def binary_search(numbers_list, number_to_find):
16+
left_index = 0
17+
right_index = len(numbers_list) - 1
18+
mid_index = 0
19+
20+
while left_index <= right_index:
21+
mid_index = (left_index + right_index) // 2
22+
mid_number = numbers_list[mid_index]
23+
24+
if mid_number == number_to_find:
25+
return mid_index
26+
27+
if mid_number < number_to_find: # this means number is in right hand side of the list
28+
left_index = mid_index + 1
29+
else: # number to find is on left hand side of the list
30+
right_index = mid_index - 1
31+
32+
return -1
33+
34+
def find_all_occurances(numbers, number_to_find):
35+
index = binary_search(numbers, number_to_find)
36+
indices = [index]
37+
# find indices on left hand side
38+
i = index-1
39+
while i >=0:
40+
if numbers[i] == number_to_find:
41+
indices.append(i)
42+
else:
43+
break
44+
i = i - 1
45+
46+
# find indices on right hand side
47+
i = index + 1
48+
while i<len(numbers):
49+
if numbers[i] == number_to_find:
50+
indices.append(i)
51+
else:
52+
break
53+
i = i + 1
54+
55+
return sorted(indices)
56+
57+
58+
if __name__ == '__main__':
59+
numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
60+
number_to_find = 15
61+
indices = find_all_occurances(numbers, number_to_find)
62+
print(f"Indices of occurances of {number_to_find} are {indices}")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from util import time_it
2+
3+
@time_it
4+
def linear_search(numbers_list, number_to_find):
5+
for index, element in enumerate(numbers_list):
6+
if element == number_to_find:
7+
return index
8+
return -1
9+
10+
@time_it
11+
def binary_search(numbers_list, number_to_find):
12+
left_index = 0
13+
right_index = len(numbers_list) - 1
14+
mid_index = 0
15+
16+
while left_index <= right_index:
17+
mid_index = (left_index + right_index) // 2
18+
mid_number = numbers_list[mid_index]
19+
20+
if mid_number == number_to_find:
21+
return mid_index
22+
23+
if mid_number < number_to_find:
24+
left_index = mid_index + 1
25+
else:
26+
right_index = mid_index - 1
27+
28+
return -1
29+
30+
def binary_search_recursive(numbers_list, number_to_find, left_index, right_index):
31+
if right_index < left_index:
32+
return -1
33+
34+
mid_index = (left_index + right_index) // 2
35+
if mid_index >= len(numbers_list) or mid_index < 0:
36+
return -1
37+
38+
mid_number = numbers_list[mid_index]
39+
40+
if mid_number == number_to_find:
41+
return mid_index
42+
43+
if mid_number < number_to_find:
44+
left_index = mid_index + 1
45+
else:
46+
right_index = mid_index - 1
47+
48+
return binary_search_recursive(numbers_list, number_to_find, left_index, right_index)
49+
50+
if __name__ == '__main__':
51+
numbers_list = [12, 15, 17, 19, 21, 24, 45, 67]
52+
number_to_find = 21
53+
54+
index = binary_search_recursive(numbers_list, number_to_find, 0, len(numbers_list))
55+
print(f"Number found at index {index} using binary search")

Algorithms/BinarySearch/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
def time_it(func):
3+
def wrapper(*args, **kwargs):
4+
start = time.time()
5+
result = func(*args,**kwargs)
6+
end = time.time()
7+
print(func.__name__ +" took " + str((end-start)*1000) + " mil sec")
8+
return result
9+
return wrapper

0 commit comments

Comments
 (0)