Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Tuple<List<int>, int> NextPermutation(int gen, List<int> x)
- {
- int n = x.Count;
- int pos,pos1;
- if (gen == 0)
- {
- for (int i = 0; i <= n - 1;i++)
- {
- x[i] = i;
- }
- gen = 1;
- return new Tuple<List<int>, int>(x,gen);
- }
- pos = -1;
- for (int i = n - 2; i >= 0; i--)
- {
- if (x[i] < x[i + 1])
- {
- pos = i;
- break;
- }
- }
- if (pos == -1)
- {
- gen = 0;
- return new Tuple<List<int>, int>(x, gen);
- }
- pos1 = pos + 1;
- for (int j = n - 1; j >=pos+1; j--)
- {
- if (x[j] > x[pos])
- {
- pos1 = j;
- break;
- }
- }
- int temp = x[pos];
- x[pos] = x[pos1];
- x[pos1] = temp;
- int a= pos+1;
- int b = n-1;
- while (a < b)
- {
- temp = x[a];
- x[a] = x[b];
- x[b] = temp;
- a++;
- b--;
- }
- return new Tuple<List<int>, int>(x, gen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement