We now discuss how we can search for values in an array.
Finding the Maximum Value
To find the greatest value in an array of integer, one needs a comparison point, a variable holding “the greatest value so far”. Once this value is set, then one “just” have to inspect each value in the array, and to update “the greatest value so far” if the value currently inspected is greater, and then to move on to the next value. Once we reach the end of the array, we know that “the greatest value so far” is actually the greatest value (period) in the array.
The problem is to find the starting point: one cannot assume that “the greatest value so far” is (what if the array contains only negative values?), so the best strategy is simply to assume that “the greatest value so far” is the first one in the array (after all, it is the greatest value we have seen so far).
Using foreach
, we have for example the following:
Finding a Particular Value
Suppose we want to set a particular Boolean variable to true
if a
particular value target
is present in an arrayy arrayExample
. The
simplest way to perform such a search is to
- Set the Boolean variable to
false
, - Inspect the values in
arrayExample
one by one, comparing them totarget
, and setting the Boolean variable totrue
if they are identical.
Note that in the particular example above, we could have stopped
exploring the array after the second index, since the target value was
found. A slightly different logic would allow to exit prematurely the
loop when the target
value is found:
This code would display:
Both codes are examples of linear (or sequential) search: the array is parsed one element after the other, and potentially all elements are inspected.
Finding a Particular Value in a Sorted Array
If the array is sorted (that is, the value at index is less than the value at index ), then the search for a particular value can be sped up by using binary search.
Sorted Arrays
A way of making sure that an array is sorted is given below. Note that,
as above when trying to find the maximum value, we decide that the array
is “sorted so far” unless proven otherwise, in which case we exit
prematurely the loop. Note also that the condition contains
index + 1 < arrayExample.Length
: we need to make sure that “the next
value” actually exists before comparing it with the current value.
Binary Search
Introduction
Binary (half-interval or logarithmic) search leverages the fact that the array is sorted to speed up the search for a particular value. It goes as follows:
The algorithm compares the target
value to the middle element of the
array.
-
If they are equal, we are done.
-
If they are not equal, then there are two cases:
- If the middle element is greater than the
target
, then the algorithm restarts, but looking for the value only in the left half of the array, - If the middle element is less than the
target
, then the algorithm restarts, but looking for the value only in the right half of the array.
- If the middle element is greater than the
-
If the search ends with the remaining half being empty, the
target
is not in the array.
First Example
An example of implementation (and of execution) is as follows:
This code would display:
Second Example
Remembering that characters are such that 'A'
is less than 'a'
, and
'a'
is less than 'b'
, we can run a binary search on a sorted array
of characters. The code below is the same algorithm as above, only the
information logged changes:
This will display:
Observe that if we were to replace start <= end
with start < end
then the algorithm would not have correctly terminated in the example
above.