Guest User

Untitled

a guest
Dec 4th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. package selection_sort;
  2.  
  3. interface LessThan<T> {
  4. boolean less(final T t1, final T t2);
  5. };
  6.  
  7. public class Main
  8. {
  9. private static <T> void selectionSort(final T[] array, final LessThan<T> lessThan) {
  10. for (int i = 0; i < array.length; ++i) {
  11. int index = i;
  12. for (int j = i + 1; j < array.length; ++j) {
  13. if (lessThan.less(array[j], array[index]))
  14. index = j;
  15. }
  16. if (index != i) {
  17. final T tmp = array[i];
  18. array[i] = array[index];
  19. array[index] = tmp;
  20. }
  21. }
  22. }
  23.  
  24. private static <T> void print(final T[] array) {
  25. for (final T i : array)
  26. System.out.print(i + " ");
  27. System.out.println();
  28. }
  29.  
  30. public static void main(final String[] args) {
  31. final Integer[] array = {7, 0, -4, 3, 1, -2, 5 };
  32.  
  33. print(array);
  34. selectionSort(array, (t1, t2) -> t1 < t2);
  35. print(array);
  36. }
  37. }
Add Comment
Please, Sign In to add comment