我正在编写一种合并排序方法,该方法可以根据频率或基于频率出现的次数对单词进行排序。从最高到最低。出于某种原因,我的单词没有按照正确的顺序排序,而且我所做的一切似乎在逻辑上是正确的并且在纸上工作。
方法:
public void mergeSortFreq(ArrayList<Term> toDo, int first, int mid, int last){
int first1 = first;
int last1 = mid;
int first2 = mid+1;
int last2 = last;
int index = 0;
ArrayList<Term> temp = new ArrayList<Term>();
int counter = 0;
while((first1 <= last1) && (first2 <= last2)){
if(toDo.get(first1).getTotalFrequency() >= (toDo.get(first2).getTotalFrequency())){
temp.add(index, toDo.get(first1));
first1++;
counter++;
}
else{
temp.add(index, toDo.get(first2));
first2++;
}
index++;
}
while(first1<=last1){
temp.add(index, toDo.get(first1));
first1++;
index++;
}
while(first2<=last2){
temp.add(index, toDo.get(first2-1));
first2++;
index++;
}
terms.clear();
for(int i=0; i<temp.size(); i++){
terms.add(temp.get(i));
}
}