合并排序不起作用
Merge Sort not working
我有一个对象 ArrayList,我试图按存储在对象中的“保存”编号对其进行排序。到目前为止我的代码写在下面
public static void mergesort(ArrayList<Object> list){
if(list.size() > 1){
//Split the list in half and create an ArrayList for each side
int q = list.size()/2;
//System.out.println(list.size() + " " + q);
ArrayList<Object> leftList = new ArrayList<Object>();
for(int i = 0; i > q; i++){
leftList.add(list.get(i));
}
ArrayList<Object> rightList = new ArrayList<Object>();
for(int j = q; j < list.size(); j++){
rightList.add(list.get(j));
}
// System.out.println(" leftList " + leftList.size() + " rightList " + rightList.size());
//sort each half of the list
//note: this will happen recursively
mergesort(leftList);
mergesort(rightList);
merge(leftList, rightList, list);
}
}
public static void merge(ArrayList<Object> leftList, ArrayList<Object> rightList, ArrayList<Object> list){
//'i' stores the index of the main array
//'l' stores the index of the left array
//'r' stores the index of the right array
int i = 0, l = 0, r = 0;
//the loop will run until one of the arraylists becomes empty
while(leftList.size() != l && rightList.size() !=r){
//if the saving of the current element of leftList is less than the rightList saving
if(leftList.get(l).getSaving() < rightList.get(r).getSaving()){
//Copy the current element into the final array
list.set(i, leftList.get(l));
i++;
l++;
}
else {
list.set(i, rightList.get(r));
i++;
r++;
}
}
while(leftList.size() != l){
list.set(i, leftList.get(l));
i++;
l++;
}
while(rightList.size() != r){
list.set(i,rightList.get(r));
i++;
r++;
}
}
出于某种原因,当我 运行 它时,我没有收到任何错误,但是,该列表仍未排序。任何建议将不胜感激。提前致谢
问题出在mergesort方法的第一个for中:
for (int i = 0; i > q; i++) {
应该是:
for (int i = 0; i < q; i++) {
检查条件..
我有一个对象 ArrayList,我试图按存储在对象中的“保存”编号对其进行排序。到目前为止我的代码写在下面
public static void mergesort(ArrayList<Object> list){
if(list.size() > 1){
//Split the list in half and create an ArrayList for each side
int q = list.size()/2;
//System.out.println(list.size() + " " + q);
ArrayList<Object> leftList = new ArrayList<Object>();
for(int i = 0; i > q; i++){
leftList.add(list.get(i));
}
ArrayList<Object> rightList = new ArrayList<Object>();
for(int j = q; j < list.size(); j++){
rightList.add(list.get(j));
}
// System.out.println(" leftList " + leftList.size() + " rightList " + rightList.size());
//sort each half of the list
//note: this will happen recursively
mergesort(leftList);
mergesort(rightList);
merge(leftList, rightList, list);
}
}
public static void merge(ArrayList<Object> leftList, ArrayList<Object> rightList, ArrayList<Object> list){
//'i' stores the index of the main array
//'l' stores the index of the left array
//'r' stores the index of the right array
int i = 0, l = 0, r = 0;
//the loop will run until one of the arraylists becomes empty
while(leftList.size() != l && rightList.size() !=r){
//if the saving of the current element of leftList is less than the rightList saving
if(leftList.get(l).getSaving() < rightList.get(r).getSaving()){
//Copy the current element into the final array
list.set(i, leftList.get(l));
i++;
l++;
}
else {
list.set(i, rightList.get(r));
i++;
r++;
}
}
while(leftList.size() != l){
list.set(i, leftList.get(l));
i++;
l++;
}
while(rightList.size() != r){
list.set(i,rightList.get(r));
i++;
r++;
}
}
出于某种原因,当我 运行 它时,我没有收到任何错误,但是,该列表仍未排序。任何建议将不胜感激。提前致谢
问题出在mergesort方法的第一个for中:
for (int i = 0; i > q; i++) {
应该是:
for (int i = 0; i < q; i++) {
检查条件..