Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. private <T> void mergeSort(List<T> list, Comparator<T> comparator) {
  2.         int divider = (list.size()) / 2;
  3.         mergeSort(list, comparator, 0, divider - 1, divider, list.size() - 1);
  4.     }
  5.  
  6.     private <T> void mergeSort(List<T> list, Comparator<T> comparator, int from0, int to0, int from1, int to1) {
  7.         final int aFrom0 = from0;
  8.         final int aSize0 = to0 - from0 + 1;
  9.         final int aSize1 = to1 - from1 + 1;
  10.         if (to0 - from0 >= 1) {
  11.             int divider = from0 + (to0 - from0) / 2;
  12.             mergeSort(list, comparator, from0, divider, divider + 1, to0);
  13.         }
  14.  
  15.         if (to1 - from0 >= 1) {
  16.             int divider = from1 + (to1 - from1) / 2;
  17.             mergeSort(list, comparator, from1, divider, divider + 1, to1);
  18.         }
  19.  
  20.         //We now have the elements divided up and at this point we definitely have only sorted elements in [from] to [to]
  21.         //We have to repeat this step as often as we have elements in first AND second list
  22.         while ((to0 - from0) >= 0 && (to1 - from1) >= 0) {
  23.             T first0 = list.get(from0);
  24.             T first1 = list.get(from1);
  25.  
  26.             int c = comparator.compare(first0, first1);
  27.             if (c <= 0) {
  28.                 //Left is smaller than right, so we can put left first and remove it
  29.                 //if the first left is the first we add, we can leave it at its place in the list and just increment from0 by one (shifting)
  30.                 from0++;
  31.             } else {
  32.                 //Left is bigger than right, so we can put the right first and remove it
  33.                 //this time we have to remove the element from the list and add it
  34.                 list.remove(from1);
  35.                 list.add(from0, first1);
  36.  
  37.                 //Since we shifted the list we have to do some operations there
  38.                 from0++;
  39.                 to0++;
  40.                 from1++;
  41.             }
  42.         }
  43.  
  44.         //Check if we still have elements in list0, if yes add them to the end of the merged list
  45.         int currentListEnd = aFrom0 + aSize1 + (aSize0 - (to0 - from0) - 1);
  46.         while (to0 - from0 >= 0) {
  47.             //Since to1 - from1 MUST be 0 now, we can see where we have to add them
  48.             T toAdd = list.get(from0);
  49.             list.remove(from0);
  50.             list.add(currentListEnd, toAdd);
  51.  
  52.             //Now we have to shift list0 to the right by one while also decrementing it by one
  53.             from0++;
  54.             currentListEnd++;
  55.         }
  56.  
  57.         //Check if we still have elements in list1, if yes add them to the end of the merged list
  58.         currentListEnd = aFrom0 + aSize0 + (aSize1 - (to1 - from1));
  59.         while (to0 - from0 >= 0) {
  60.             //Since to0 - from0 MUST be 0 now, we can see where we have to add them
  61.             T toAdd = list.get(from1);
  62.             list.remove(from1);
  63.             list.add(currentListEnd, toAdd);
  64.  
  65.             //Now we have to shift list1 to the right by one while also decrementing it by one
  66.             from1++;
  67.             currentListEnd++;
  68.         }
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement