This gives you next permutation:
bool Increase(int[] values) {
// locate the last item which is smaller than the following item
int pos = values.Length - 2;
while (pos >= 0 && values[pos] > values[pos + 1]) pos--;
// if not found we are done
if (pos == -1) return false;
// locate the item next higher in value
int pos2 = values.Length - 1;
while (values[pos2] < values[pos]) pos2--;
// put the higher value in that position
int temp = values[pos];
values[pos] = values[pos2];
values[pos2] = temp;
// reverse the values to the right
Array.Reverse(values, pos + 1, values.Length - pos - 1);
return true;
}
Edit:
Changed Array.Sort to Array.Reverse. The items are always in descending order and should be in ascending order, so they give the same result.