Someone asks you : tell me the 10 most popular people. If you get the indexes of these ten (in an array0, then you do a simple for loop to 10 and dump out the name_ArrayList.get(i) to spit out the answer.
How do you get the indexes of the top N? Here's how :
public int[] bestN(int N){
int [] maxes = new int[N];
int [] inds = new int[N];
// N passes.
for( int ptr = 0; ptr < N; ptr++){
maxes[ptr] = 0;
inds[ptr] = 0;
for( int i = 0; i < FOM_list.size(); i++ ){
if( FOM_list.get(i) > maxes[ptr] ){
if( (0 ==ptr) || (ptr > 0 && FOM_list.get(i) < maxes[ptr-1]) ){
// if it's the first item, nothing else to consider. Go ahead and enter. Else, ensure that this
// entry is not higher (in FOM) than the one that's supposed to be better than it 😊
maxes[ptr] = FOM_list.get(i);
inds[ptr] = i;
}
}
}
}
return inds;
}
And then :
createTheArrayLists();
int [] best10 = new int[10];
best10 = bestN(10);
System.out.println("Most popular bloggers:");
for( int i = 0; i< 10; i++){
System.out.println(bloggerAList.get(best10[i]) + ": " + followers.get(best10[i]));
}
No comments:
Post a Comment