Java Reference
In-Depth Information
implement the binary search algorithm on the array candidatesName . We will write
the definition of the method binSearch shortly. First we discuss how to update the
array votesByRegion .
Suppose that the three arrays are as shown in Figure 14-19.
candidatesName
votesByRegion
totalVotes
[0]
0
10
76
0
80
100
[1]
0
0
13
45
0
0
[2]
50
56
0
0
78
0
[3]
0
0
0
0
0
20
[0]
Ashley
Danny
Donald
Mia
Mickey
Peter
[0]
[0]
0
0
0
0
0
0
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-19 Arrays candidatesName , votesByRegion , and totalVotes
Further suppose that the next entry read from the input file is:
Donald 2 35
Next, we locate the row in the preceding grid that corresponds to this candidate. To
find the row, we search the array candidatesName to find the row that corresponds
to this name. Now Donald corresponds
to row number 2 in the array
candidatesName , as shown in Figure 14-20.
Donald
region = 2
candidatesName
votesByRegion
totalVotes
[0]
0
10
76
0
80
100
[1]
0
0
13
45
0
0
[2]
50
56
0
0
78
0
[3]
0
0
0
0
0
20
[0]
Ashley
Danny
Donald
Mia
Mickey
Peter
[0]
0
0
0
0
0
0
[0]
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-20 Position of Donald and region = 2
Search WWH ::




Custom Search