Java Reference
In-Depth Information
START
Get search
data from user
Set boolean flag,
found, to false
Set parameters
of loop: 1 to
end of list
Yes
Is list
exhausted?
No
Yes
Found?
No
Display
not found
message
Display
found
message
Does search
data match list
data at index?
Yes
Found = true
No
STOP
Move to next
in list
FIGURE 7-32
A linear search may be inefficient, however, for large amounts of data. For
example, if the array had 10,000 unordered members, the linear search may have
to look at 9,999 items before finding the desired element. A binary search , suited
for large arrays of sorted data, is more efficient because it reduces the number of
times the program must compare search data with stored data. A binary search is
similar to the children's game of High-Low. A player might be asked to think of a
number between 1 and 100. Most players would respond with the answer, 50, at
which time they would be told to guess higher or lower. The process repeats as the
player tries to narrow the guesses down to the right number.
 
Search WWH ::




Custom Search