From charlesreid1

(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...")
 
Line 32: Line 32:




==Flags=
==Flags==


[[Category:Stacks]]
[[Category:Stacks]]

Revision as of 18:45, 4 June 2017

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