You are using a binary search algorithm to search a sorted array for a given value. Describe
Fantastic news! We've Found the answer you've been seeking!
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 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.
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Posted Date: