Implementing Binary Search of an Array
We'll cover the following...
Let's think about binary search on a sorted array. Many programming languages already provide methods for determining whether a given element is in an array and, if it is, its location. But we want to implement it ourselves, to understand how you can implement such methods. Here's an array of the first 25 prime numbers, in order:
Suppose we want to know whether the number
We might also want to know how many primes are smaller than
The position of an element in an array is known as its index. Array indices start at
Looking at the example below, we can read the array of prime numbers from left to right, one at a time, until we find the number
Once we know that the prime number