Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. private static Tuple<List<int>, int> NextPermutation(int gen, List<int> x)
  2. {
  3. int n = x.Count;
  4. int pos,pos1;
  5.  
  6. if (gen == 0)
  7. {
  8. for (int i = 0; i <= n - 1;i++)
  9. {
  10. x[i] = i;
  11. }
  12.  
  13. gen = 1;
  14. return new Tuple<List<int>, int>(x,gen);
  15. }
  16.  
  17. pos = -1;
  18. for (int i = n - 2; i >= 0; i--)
  19. {
  20. if (x[i] < x[i + 1])
  21. {
  22. pos = i;
  23. break;
  24. }
  25. }
  26.  
  27. if (pos == -1)
  28. {
  29. gen = 0;
  30. return new Tuple<List<int>, int>(x, gen);
  31. }
  32.  
  33. pos1 = pos + 1;
  34. for (int j = n - 1; j >=pos+1; j--)
  35. {
  36. if (x[j] > x[pos])
  37. {
  38. pos1 = j;
  39. break;
  40. }
  41. }
  42.  
  43. int temp = x[pos];
  44. x[pos] = x[pos1];
  45. x[pos1] = temp;
  46.  
  47. int a= pos+1;
  48. int b = n-1;
  49. while (a < b)
  50. {
  51. temp = x[a];
  52. x[a] = x[b];
  53. x[b] = temp;
  54. a++;
  55. b--;
  56. }
  57. return new Tuple<List<int>, int>(x, gen);
  58. }
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement