Java Reference
In-Depth Information
private boolean search( int first, int last, T desiredItem)
{
boolean found;
if (first > last)
found = false ; // no elements to search
else if (desiredItem.equals(list[first]))
found = true ;
else
found = search(first + 1, last, desiredItem);
return found;
} // end search
FIGURE 18-3
A recursive sequential search of an array that (a) finds its target;
(b) does not find its target
(a) A search for 8
(b) A search for 6
Look at the first entry, 9:
9
Look at the first entry, 9:
9
5
8
4
7
5
8
4
7
8
9, so search the next subarray.
6
9, so search the next subarray.
Look at the first entry, 5:
5
Look at the first entry, 5:
5
8
4
7
8
4
7
8
5, so search the next subarray.
6
5, so search the next subarray.
Look at the first entry, 8:
8
Look at the first entry, 8:
8
4
7
4
7
8
8, so the search has found 8.
6 8, so search the next subarray.
Look at the first entry, 4:
4
7
6 4, so search the next subarray.
Look at the first entry, 7:
7
6
7, so search an empty array.
No entries are left to consider, so the
search ends. 6 is not in the array.
 
Search WWH ::




Custom Search