From charlesreid1

Revision as of 18:44, 4 June 2017 by Admin (talk | contribs) (Created page with "==Algorithm for finding permutations of subsets in lexicographic order, no recursion== Here are the steps: To find permutations, in lexicographic order: 1. Sort the list of...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Algorithm for finding permutations of subsets in lexicographic order, no recursion

Here are the steps:

To find permutations, in lexicographic order:

1. Sort the list of items and print this as the first permutation

2. Let i be the last index such that input[i] < input[i+1] (if no such index, we are done)

3. Let j be the last index such that input[i] < input[j]

4. Swap input[i] with input[j]

5. Reverse input[i+1] through input[input.length-1]

Example

Pass the flag "CHARLES"

First permutation is sorted: ACEHLRS

Second string is ACEHLSR

Third string is ACEHRLS

etc.

Code

=Flags