How would I implement a binary search using just an array?
Binary Search in Array
algorithmarraysbinary-searchlanguage-agnosticsearch
Related Topic
- Javascript – How to check if an array includes a value in JavaScript
- Javascript – How to append something to an array
- Php – Deleting an element from an array in PHP
- Javascript – Sort array of objects by string property value
- Java – How to declare and initialize an array in Java
- Javascript – How to check if an object is an array?
- Javascript – How to remove a specific item from an array
- Javascript – For-each over an array in JavaScript
Best Answer
Ensure that your array is sorted since this is the crux of a binary search.
Any indexed/random-access data structure can be binary searched. So when you say using "just an array", I would say arrays are the most basic/common data structure that a binary search is employed on.
You can do it recursively (easiest) or iteratively. Time complexity of a binary search is O(log N) which is considerably faster than a linear search of checking each element at O(N). Here are some examples from Wikipedia: Binary Search Algorithm:
Recursive:
Iterative: