TITLE: Find all permutations in increasing order
QUESTION [7 upvotes]: Given a set of distinct numbers, say, {1, 2, 3, 4, 5, 6}, find all permutations containing 3 numbers. All the permutations have to be in ascending order.
For e.g., some correct permutations would be {1, 2, 3}, {2, 4, 6}, etc. {2, 3, 1} would be incorrect because it is not in ascending order.
How does one go about solving these kinds of questions? Say, instead of choosing 3 numbers, we had to choose 4 or 5, or maybe the given set would be different, what would be the general appraach?
Thanks.
P.S.: You don't just have to tell the number of possible permutations, but also list them.

REPLY [4 votes]: To print a list of all increasing permutations consisting of three elements from the set $\{1,2,3,\ldots,n\},$ use a nested loop:
for i = 1 to n-2 {
  for j = i+1 to n-1 {
    for k = j+1 to n {
      print {i,j,k}
    }
  }
}

For $k$ elements, you could hold the permutation in an array $p$ of length $k:$
for i = 1 to k {p[i] = i};   /* initialize p to {1,2,...,k} */
repeat {
  print p;
  i = k;
  /* find the rightmost incrementable element.  p[i] can't exceed n+i-k. */
  while i > 0 and p[i] == n+i-k {
    i = i - 1
  };
  if i > 0 {    /* if there exists an incrementable element... */
    p[i] = p[i] + 1;     /* increment it */
    for j = i + 1 to k {    /* and set all subsequent elts to their min values */
      p[j] = p[j-1] + 1
    } 
  }
} until i == 0

If the set consists of distinct elements other than $1,\ 2,\ \ldots, n,$ then sort the elements: $a_1< a_2<\ldots< a_n.$  Use the above algorithm, but instead of printing $p,$ print the elements indexed by $p,$ that is $a_{p_1},\ a_{p_2},\ \ldots, a_{p_k}.$
Multisets: If the elements are not distinct, that is, if we are given a multiset rather than a set, then the algorithm above cannot be used as is, since it will produce duplicates.  Assume once again that we have sorted the elements: $a_1\le a_2\le\ldots\le a_n.$
Given the multiset $\{3,5,5,5,6,6\},$ for example, we have $a_1=3,$ $a_2=a_3=a_4=5,$ $a_5=a_6=6.$  We would not, for instance, want to print both $a_1a_2a_3a_5$ and $a_1a_2a_4a_6,$ since they are the same thing: $3556.$  The fix for this is to require that if we include two of $a_2,\ a_3,\ a_4,$ we include the two of least index, namely $a_2$ and $a_3,$ rather than $a_2$ and $a_4$ or $a_3$ and $a_4.$  Likewise, if we include one of $a_5,\ a_6,$ we include the one of least index: $a_5$ rather than $a_6.$
To this end, we modify the algorithm above by defining a least increment for each of the $n$ sorted elements of the multiset: $r_1=1,$ $r_2=3,$ $r_3=2,$ $r_4=1,$ $r_5=2,$ $r_6=1.$  The modified algorithm is the following:
/* assume the elements a[1], a[2], ..., a[n] to be sorted in ascending order */
/* compute least increments */
r[n] = 1;
for i = n-1 downto 1 {
  if a[i] == a[i+1] {r[i] = r[i+1] + 1} else {r[i] = 1}
};
/* initialize p to {1,2,...,k} */
for i = 1 to k {p[i] = i};
repeat {
  for j = 1 to k {print a[p[j]]};
  print <newline>;
  i = k;
  /* find the rightmost incrementable element.  p[i] can't exceed n+i-k. */
  while i > 0 and p[i]+r[p[i]] > n+i-k {
    i = i - 1
  };
  if i > 0 {    /* if there exists an incrementable element... */
    p[i] = p[i] + r[p[i]];     /* increment it */
    for j = i + 1 to k {    /* and set all subsequent elts to their min values */
      p[j] = p[j-1] + 1
    } 
  }
} until i == 0