简体   繁体   中英

Java ArrayList merge sort algorithm

I'm having some issues with my merge sort algorithm in Java. It seems to be doing a lot of weird things now and I'm having trouble getting it to work. I believe the issue might lie somewhere in the mergeArrayLists function, but I'm not sure. Any help would be appreciated!

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;
   }
}

You have an off by one error.

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));
}

Your lists should be index < size not size -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM