首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java ArrayList归并排序算法

Java ArrayList归并排序算法
EN

Stack Overflow用户
提问于 2012-03-03 00:36:44
回答 1查看 4.1K关注 0票数 0

我在Java中的合并排序算法有一些问题。它现在似乎做了很多奇怪的事情,我在让它工作时遇到了麻烦。我相信问题可能出在mergeArrayLists函数的某个地方,但我不确定。任何帮助都将不胜感激!

代码语言:javascript
复制
public class MergeSort extends Sort {

   public MergeSort() {
   }

   // Inherited from Sort
   public <T extends Comparable<T>> void sortArrayList(ArrayList<T> arrayList) {
      arrayList = mergeSort(arrayList);
   }

   // Returns the sorted form of the given array list (sorted via the merge sort algorithm)
   public <T extends Comparable<T>> ArrayList<T> mergeSort(
         ArrayList<T> arrayList) {
      if (arrayList.size() <= 1) {
         return arrayList;
      } else {
         ArrayList<T> firstList = new ArrayList<T>();
         ArrayList<T> secondList = new ArrayList<T>();

         for (int i = 0; i < arrayList.size(); i++) {
            T thisValue = arrayList.get(i);
            if (i < arrayList.size() / 2) {
               firstList.add(thisValue);
            } else {
               secondList.add(thisValue);
            }
         }
         //System.out.println(firstList+" "+mergeSort(firstList));
         ArrayList<T> firstSort = mergeSort(firstList);
         ArrayList<T> secondSort = mergeSort(secondList);
         return mergeArrayLists(firstSort, secondSort);
      }
   }

   // Merges two array lists together, in order
   public <T extends Comparable<T>> ArrayList<T> mergeArrayLists(
         ArrayList<T> firstList, ArrayList<T> secondList) {
      ArrayList<T> resultList = new ArrayList<T>();

      int firstIndex, secondIndex = 0;
      for (firstIndex = 0; firstIndex < firstList.size() - 1; firstIndex++) {
         while (secondIndex < secondList.size() - 1) {
            if (firstList.get(firstIndex)
                  .compareTo(secondList.get(secondIndex)) < 0) {
               break;
            } else {
               resultList.set(firstIndex + secondIndex,
                     secondList.get(secondIndex));
               secondIndex++;
            }
         }
         resultList.set(firstIndex + secondIndex, firstList.get(firstIndex));
      }
      System.out.println(firstList + " + " + secondList + " = " + resultList);

      return resultList;
   }
}
EN

回答 1

Stack Overflow用户

发布于 2012-03-03 00:39:44

你有一个错误的off。

代码语言:javascript
复制
for(firstIndex=0; firstIndex<firstList.size(); firstIndex++) {
    while(secondIndex < secondList.size()) {
        if(firstList.get(firstIndex).compareTo( secondList.get(secondIndex) ) < 0) {
            break;
        }
        else {
            resultList.set(firstIndex + secondIndex, secondList.get(secondIndex));
            secondIndex++;
        }
    }
    resultList.set(firstIndex + secondIndex, firstList.get(firstIndex));
}

你的列表应该是index < size而不是size -1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9536936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档