University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.java 664B

1234567891011121314151617181920212223242526272829303132
  1. class Util {
  2. // Swap two elements
  3. static void swap(int[] a, int i, int j) {
  4. int tmp = a[j];
  5. a[j] = a[i];
  6. a[i] = tmp;
  7. }
  8. // Insert a[i] into a[min..min+k]
  9. static void insertAt(int[] a, int min, int k, int i) {
  10. if (a[i] > a[min + k - 1]) {
  11. swap(a, i, min + k - 1);
  12. int j = min + k - 2;
  13. while (j >= min && a[j + 1] > a[j]) {
  14. swap(a, j + 1, j);
  15. j -= 1;
  16. }
  17. }
  18. }
  19. // Insertion sort from a[0] to a[k-1], biggest to smallest
  20. static void insertionSortReverse(int[] a, int min, int max) {
  21. for (int i = min + 1; i < max; ++i) {
  22. int j = i - 1;
  23. while (j >= min && a[j + 1] > a[j]) {
  24. swap(a, j, j + 1);
  25. j -= 1;
  26. }
  27. }
  28. }
  29. }