Question: You are using a binary search algorithm to search a sorted array for a given value. Describe your reasoning and organize the steps in defining
You are using a binary search algorithm to search a sorted array for a given value. Describe your reasoning and organize the steps in defining bases cases and the general case by listing the order in which they should occur:
- Assuming that the array is not empty, formulate the general case to search both higher and lower for the value, making a recursive call to our search method.
- Define the base case such that if the value of the middle element is the value we seek, it will return its index.
- Return -1 when the array is empty (i.e., a base case is reached).
- Continue searching; the part of the array being searched will shrink until it is empty.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
