|
| 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 | +# This is because the array is not sorted in order from lowest to highest. |
| 7 | +# Once it splits the first time, it starts looking in the [1,4,6] range and doesn't find 5 |
| 8 | + |
| 9 | +# 1. Find index of all the occurances of a number from sorted list |
| 10 | + |
| 11 | +# ``` |
| 12 | +# numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56] |
| 13 | +# number_to_find = 15 |
| 14 | +# ``` |
| 15 | +# This should return 5,6,7 as indices containing number 15 in the array |
| 16 | + |
| 17 | +from util import time_it |
| 18 | + |
| 19 | +@time_it |
| 20 | +def linear_search(numbers_list, number_to_find): |
| 21 | + for index, element in enumerate(numbers_list): |
| 22 | + if element == number_to_find: |
| 23 | + return index |
| 24 | + return -1 |
| 25 | + |
| 26 | +@time_it |
| 27 | +def binary_search(numbers_list, number_to_find): |
| 28 | + left_index = 0 |
| 29 | + right_index = len(numbers_list) - 1 |
| 30 | + mid_index = 0 |
| 31 | + |
| 32 | + while left_index <= right_index: |
| 33 | + mid_index = (left_index + right_index) // 2 |
| 34 | + mid_number = numbers_list[mid_index] |
| 35 | + |
| 36 | + if mid_number == number_to_find: |
| 37 | + return mid_index |
| 38 | + |
| 39 | + if mid_number < number_to_find: |
| 40 | + left_index = mid_index + 1 |
| 41 | + else: |
| 42 | + right_index = mid_index - 1 |
| 43 | + |
| 44 | + return -1 |
| 45 | + |
| 46 | +def binary_search_recursive(numbers_list, number_to_find, left_index, right_index): |
| 47 | + if right_index < left_index: |
| 48 | + return -1 |
| 49 | + |
| 50 | + mid_index = (left_index + right_index) // 2 |
| 51 | + if mid_index >= len(numbers_list) or mid_index < 0: |
| 52 | + return -1 |
| 53 | + |
| 54 | + mid_number = numbers_list[mid_index] |
| 55 | + |
| 56 | + if mid_number == number_to_find: |
| 57 | + return mid_index |
| 58 | + |
| 59 | + if mid_number < number_to_find: |
| 60 | + left_index = mid_index + 1 |
| 61 | + else: |
| 62 | + right_index = mid_index - 1 |
| 63 | + |
| 64 | + return binary_search_recursive(numbers_list, number_to_find, left_index, right_index) |
| 65 | + |
| 66 | +#this should run the binary search, find the index, and then recursively run the search on both the right and left side |
| 67 | +def binary_search_multiple(numbers_list, number_to_find): |
| 68 | + |
| 69 | + index = binary_search(numbers_list,number_to_find) |
| 70 | + result_indices = [index] |
| 71 | + |
| 72 | + # find all indices on the left |
| 73 | + i = index - 1 |
| 74 | + while i>=0: |
| 75 | + if numbers_list[i] == numbers_list[index]: |
| 76 | + result_indices.append(i) |
| 77 | + else: |
| 78 | + break |
| 79 | + i = i-1 |
| 80 | + |
| 81 | + # find all indices on the right |
| 82 | + i = index + 1 |
| 83 | + while i<len(numbers_list): |
| 84 | + if numbers_list[i] == numbers_list[index]: |
| 85 | + result_indices.append(i) |
| 86 | + else: |
| 87 | + break |
| 88 | + i = i+1 |
| 89 | + |
| 90 | + return sorted(result_indices) |
| 91 | + |
| 92 | +numbers_list = [12, 15, 17, 19, 21, 21, 21, 21, 24, 45, 67] |
| 93 | +number_to_find = 21 |
| 94 | + |
| 95 | +index = binary_search_multiple(numbers_list, number_to_find) |
| 96 | +print(f"Number found at index {index} using binary search") |
| 97 | + |
| 98 | +numbers = [1,4,6,9,11,15,15,15,15,17,21,34,34,56] |
| 99 | +number_to_find = 15 |
| 100 | + |
| 101 | +index = binary_search_multiple(numbers, number_to_find) |
| 102 | +print(f"Number found at index {index} using binary search") |
| 103 | + |
| 104 | +#Lesson: I was approaching it wrong. If something isn't working, scratch the approach. |
| 105 | +#Lesson #2: Try the simplest solution first. Although in this case it's a bit ugly since you're just doing a linear search after your binary search |
0 commit comments