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.

Sequential.java 605B

123456789101112131415161718192021222324
  1. import java.util.Random;
  2. import java.util.Arrays;
  3. class Sequential implements A2Solver {
  4. // The actual A2 algorithm, which ensures the array is sorted
  5. // biggest to smallest from in A[0] to A[k-1], but doesn't sort
  6. // the entire array
  7. public void solveA2(int[] a, int k) {
  8. Util.insertionSortReverse(a, 0, Math.min(k, a.length));
  9. // Insert the biggest values in a[k..n] into a[0..k-1]
  10. for (int i = k; i < a.length; ++i) {
  11. if (a[i] > a[k-1]) {
  12. Util.swap(a, i, k-1);
  13. int j = k - 2;
  14. while (j >= 0 && a[j + 1] > a[j]) {
  15. Util.swap(a, j + 1, j);
  16. j -= 1;
  17. }
  18. }
  19. }
  20. }
  21. }