/*
  Write the following two generic methods using merge sort. The first method
  sorts the elements using the Comparable interface and the second uses the
  Comparator interface.

  public static <E extends Comparable<E>> void mergeSort(E[] list)
  public static <E> void mergeSort(E[] list, Comparator<? super E> comparator)
*/

import java.util.Arrays;
import java.util.Comparator;

public class E23_02 {
  public static void main(String[] args) {
    Integer[] list = {15, 25, 2, 4, 18, 92, -10, 54};
    Integer[] copy1 = Arrays.copyOf(list, list.length);
    Integer[] copy2 = Arrays.copyOf(list, list.length);

    mergeSort(copy1);
    System.out.println("mergeSort (comparable)");
    printList(copy1);

    mergeSort(copy2, new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
        if (o1.compareTo(o2) > 0) {
          return -1;
        } else if (o1.compareTo(o2) < 0) {
          return 1;
        }
        return 0;
      }
    });
    System.out.println("mergeSort (comparator - descending)");
    printList(copy2);
  }

  public static <E extends Comparable> void mergeSort(E[] list) {
    if (list.length > 1) {
      E[] firstHalf = Arrays.copyOfRange(list, 0, list.length / 2);
      E[] secondHalf = Arrays.copyOfRange(list, list.length / 2, list.length);
      mergeSort(firstHalf);
      mergeSort(secondHalf);

      int current1 = 0;
      int current2 = 0;
      int current3 = 0;

      while (current1 < firstHalf.length && current2 < secondHalf.length) {
        if (firstHalf[current1].compareTo(secondHalf[current2]) < 0) {
          list[current3++] = firstHalf[current1++];
        } else {
          list[current3++] = secondHalf[current2++];
        }
      }

      while (current1 < firstHalf.length) {
        list[current3++] = firstHalf[current1++];
      }

      while (current2 < secondHalf.length) {
        list[current3++] = secondHalf[current2++];
      }
    }
  }

  public static <E> void mergeSort(E[] list, Comparator<? super E> comparator) {
    if (list.length > 1) {
      E[] firstHalf = Arrays.copyOfRange(list, 0, list.length / 2);
      E[] secondHalf = Arrays.copyOfRange(list, list.length / 2, list.length);
      mergeSort(firstHalf, comparator);
      mergeSort(secondHalf, comparator);

      int current1 = 0;
      int current2 = 0;
      int current3 = 0;

      while (current1 < firstHalf.length && current2 < secondHalf.length) {
        if (comparator.compare(firstHalf[current1], secondHalf[current2]) < 0) {
          list[current3++] = firstHalf[current1++];
        } else {
          list[current3++] = secondHalf[current2++];
        }
      }

      while (current1 < firstHalf.length) {
        list[current3++] = firstHalf[current1++];
      }

      while (current2 < secondHalf.length) {
        list[current3++] = secondHalf[current2++];
      }
    }
  }

  public static <E> void printList(E[] list) {
    for (E elem: list) {
      System.out.print(elem + " ");
    }
    System.out.println();
  }
}

@@ -1113,16 +1113,19 @@
 mparable
+%3CE%3E
 %3E void m
@@ -1153,809 +1153,54 @@
     
-if (list.length %3E 1) %7B%0A      E%5B%5D firstHalf = Arrays.copyOfRange(list, 0, list.length / 2);%0A      E%5B%5D secondHalf = Arrays.copyOfRange(list, list.length / 2, list.length);%0A      mergeSort(firstHalf);%0A      mergeSort(secondHalf);%0A%0A      int current1 = 0;%0A      int current2 = 0;%0A      int current3 = 0;%0A%0A      while (current1 %3C firstHalf.length && current2 %3C secondHalf.length) %7B%0A        if (firstHalf%5Bcurrent1%5D.compareTo(secondHalf%5Bcurrent2%5D) %3C 0) %7B%0A          list%5Bcurrent3++%5D = firstHalf%5Bcurrent1++%5D;%0A        %7D else %7B%0A          list%5Bcurrent3++%5D = secondHalf%5Bcurrent2++%5D;%0A        %7D%0A      %7D%0A%0A      while (current1 %3C firstHalf.length) %7B%0A        list%5Bcurrent3++%5D = firstHalf%5Bcurrent1++%5D;%0A      %7D%0A%0A      while (current2 %3C secondHalf.length) %7B%0A        list%5Bcurrent3++%5D = secondHalf%5Bcurrent2++%5D;%0A      %7D%0A    %7D
+mergeSort(list, new AscendingComparator%3CE%3E());
 %0A  %7D
@@ -2270,11 +2270,308 @@
 n();%0A  %7D
+%0A%0A  private static class AscendingComparator%3CE extends Comparable%3CE%3E%3E%0A    implements Comparator%3CE%3E %7B%0A    @Override%0A    public int compare(E o1, E o2) %7B%0A      if (o1.compareTo(o2) %3C 0) %7B%0A        return -1;%0A      %7D else if (o1.compareTo(o2) %3E 0) %7B%0A        return 1;%0A      %7D%0A      return 0;%0A    %7D%0A  %7D
 %0A%7D%0A

Fix references to raw types. Make list-only mergeSort use the Comparator version with an ascending order comparator object.