Java Reference
In-Depth Information
temp = cNames[i];
cNames[i] = cNames[min];
cNames[min] = temp;
}
}
After a call to this method, the arrays are as shown in Figure 14-18.
candidatesName
votesByRegion
totalVotes
[0]
0
0
0
0
0
0
[1]
0
0
0
0
0
0
[2]
0
0
0
0
0
0
[3]
0
0
0
0
0
0
[0]
0
0
0
0
0
0
[0]
Ashley
Danny
Donald
Mia
Mickey
Peter
[0]
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-18 Arrays candidatesName , votesByRegion , and totalVotes after sorting
names
Processing the voting data is quite straightforward. Each entry in the file voteData.txt
is in the following form:
Process
Voting Data
candidatesName regionNumber numberOfVotesForTheCandidate
The general algorithm to process the voting data follows.
For each entry in the file voteData.txt :
1. Get
the
candidatesName ,
regionNumber ,
and
numberOfVotesForTheCandidate .
2. Find the row number in the array candidatesName corresponding
to this candidate. This will give the corresponding row number in
the array votesByRegion for this candidate.
3. Find the column number in the array votesByRegion correspond-
ing to this regionNumber .
4. Update the appropriate entry in the array votesByRegion by adding
numberOfVotesForTheCandidate.
Step 2 requires us to search the array candidatesName to find the location, that is,
row number, of a particular candidate. Because the array candidatesName is sorted,
we can use the binary search algorithm to find the row number corresponding to a
particular candidate. Therefore, the program also includes the method binSearch to
1
4
 
Search WWH ::




Custom Search