Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 4th, 2013  |  syntax: None  |  size: 0.76 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class MergeSort<E extends Comparable<E>>
  2.   implements Sorter<E> {
  3.  
  4.   private E[] array, array2;
  5.  
  6.   public void sort (E[] array) {
  7.     this.array = array;
  8.     array2 = array.clone();
  9.     sort(0, array.length-1);
  10.   }
  11.  
  12.   private void sort(int left, int right) {
  13.     if (left >= right)
  14.       return;
  15.    
  16.     int middle = (left + right) / 2;
  17.     sort(left, middle);
  18.     sort(middle+1, right);
  19.    
  20.     int i = left;
  21.     int a = left;
  22.     int b = middle+1;
  23.     while (a <= middle || b <= right) {
  24.       // If both a <= middle and b <= right
  25.       // copy the smaller of array[a] or array[b] to array2[i]
  26.       // Otherwise just copy the remaining elements to array2
  27.     }
  28.    
  29.     System.arraycopy(array2, left, array, left, right - left + 1);
  30.   }
  31. }
clone this paste RAW Paste Data