I tried to create a generic next_permutation()
function which would do the same thing as the C++ next_permutation()
function. The compiler says:
bad operand types for binary operator '>' and '<',
at the following lines:
if (data.get(last) < data.get(last + 1))
if (if data.get(i) > data.get(last))
How do I resolve this?
public static <T> boolean findNextPermutation(List<T> data)
{
if (data.size() <= 1)
return false;
int last = data.size() - 2;
while (last >= 0) {
if (data.get(last) < data.get(last + 1)) {
break;
}
last--;
}
if (last < 0)
return false;
int nextGreater = data.size() - 1;
for (int i = data.size() - 1; i > last; i--) {
if (data.get(i) > data.get(last)) {
nextGreater = i;
break;
}
}
data = swap(data, nextGreater, last);
data = reverse(data, last + 1, data.size() - 1);
return true;
}