Advertisement
Alsturm

Untitled

Jul 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public static <T> void sort(Comparator<T> comparator, T... array){
  2. sort(comparator, 0, array.length - 1, array);
  3. }
  4.  
  5. public static <T> void sort(Comparator<T> comparator, int left, int right, T... array) {
  6. if (left < right) {
  7. int separatingIndex = partition(comparator, left, right, array);
  8. sort(comparator, left, separatingIndex, array);
  9. sort(comparator,separatingIndex + 1, right, array);
  10. }
  11.  
  12. }
  13. private static <T> int partition(Comparator<T> comparator, int left, int right, T... array){
  14. T separator = array[(left + right) / 2];
  15. int i = left;
  16. int j = right;
  17. while (i <= j) {
  18. while (comparator.compare(array[i], separator) > 0) i++;
  19. while (comparator.compare(array[j], separator) < 0) j--;
  20.  
  21. if (i <= j) {
  22. T tmp = array[i];
  23. array[i++] = array[j];
  24. array[j--] = tmp;
  25. }
  26. }
  27. return --i;
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement