Hide
Don't get spied on – We respect your privacy and provide numerous options to protect it. Join Siafoo Now or Learn More

Next lexicographic permutation of int array. Atom Feed -1

In Brief Very simple lexicographic permutation in C#.
# 's
 1      static public int[] nextLexiPermutation(int[] s)
2 {
3 int i = -1, j = 0;
4
5 for (int x = s.Length - 2; x >= 0; x--)
6 if (s[x] < s[x + 1])
7 {
8 i = x;
9 break;
10 }
11
12 if (-1 == i)
13 return null;
14
15 for (int x = s.Length - 1; x > i; x--)
16 if (s[x] > s[i])
17 {
18 j = x;
19 break;
20 }
21
22 // Swapping elements pointed by i and j;
23 int temp = s[i];
24 s[i] = s[j];
25 s[j] = temp;
26
27 // Reversing elements after i
28 Array.Reverse(s, i + 1, s.Length - (i + 1));
29 return s;
30 }

Very simple lexicographic permutation in C#.