Binary Search
File Name: binary_search.py ```python from stylepy import h1,h2,h3,h4,h5,h6 def binary_search_in_recursive(input_array, search_value, left, right): if(left > right): # Reached the end and unable to search. return False
1 2 3 4 5 6 7 |
|
def binary_search_in_iterative(input_array, search_value): left = 0 right = len(input_array) - 1
1 2 3 4 5 6 7 8 9 10 |
|
h3("""
Sample Problem: Imagine you are working with a dataset of recorded temperatures in a city, logged every hour over a week. This dataset is sorted by the temperature values. You want to find the first occurrence of a specific temperature, say 15°C, in this dataset. """)
h1('>>> Binary Search in Recursive');
input_array = [10, 11, 12, 13, 14, 15, 15, 15, 16, 17, 18, 19, 20] left_index = 0 right_index = len(input_array) - 1 print(f"binary_search_in_recursive(19) is {binary_search_in_recursive(input_array, 19, left_index, right_index)}"); print(f"binary_search_in_recursive(18) is {binary_search_in_recursive(input_array, 18, left_index, right_index)}");
h1('\n\n>>> Binary Search in Recursive');
print(f"binary_search_in_iterative(15) is {binary_search_in_iterative(input_array, 15)}"); print(f"binary_search_in_iterative(21) is {binary_search_in_iterative(input_array, 21)}");```
Documentation
h1('>>> Binary Search in Recursive');
input_array = [10, 11, 12, 13, 14, 15, 15, 15, 16, 17, 18, 19, 20] left_index = 0 right_index = len(input_array) - 1 print(f"binary_search_in_recursive(19) is {binary_search_in_recursive(input_array, 19, left_index, right_index)}"); print(f"binary_search_in_recursive(18) is {binary_search_in_recursive(input_array, 18, left_index, right_index)}");
h1('\n\n>>> Binary Search in Recursive');
print(f"binary_search_in_iterative(15) is {binary_search_in_iterative(input_array, 15)}"); print(f"binary_search_in_iterative(21) is {binary_search_in_iterative(input_array, 21)}");