/**
* Re-orders the elements of the argument list t so that
* all of the negative elements of t are in the front part
* of the list and all of the positive elements of t are
* in the back part of the list. There is no guarantee of
* the ordering of the positive elements or of the negative
* elements.
*
* Zero is considered to be a positive number for the purposes
* of this method.
*
* For example, a possible solution results in the following:
*
* if t is [-1, 2] then partition(t) makes t [-1, 2]
* if t is [2, -1] then partition(t) makes t [-1, 2]
* if t is [8, -2, 1] then partition(t) makes t [-2, 1, 8]
* if t is [1, -5, -9, 4] then partition(t) makes t [-5, -9, 4, 1]
* if t is [-3, 7, -1, 3, -5] then partition(t) makes t [-3, -1, -5, 7, 3]
*
* @param t a list of integers
*/
public static void partition(List<Integer> t) {
if (t.size() < 2) { // base case
return;
}
int first = t.get(0);
if (first < 0) { // recursive case 1
partition(t.subList(1, t.size()));
}
else { // recursive case 2
t.add(first);
t.remove(0);
partition(t.subList(1, t.size() - 1));
}
}