StacksQueues/Subsets/Java
From charlesreid1
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
| Algorithms Part of Computer Science Notes
Series on Algorithms
Algorithms/Sort · Algorithmic Analysis of Sort Functions · Divide and Conquer · Divide and Conquer/Master Theorem Three solid O(n log n) search algorithms: Merge Sort · Heap Sort · Quick Sort Algorithm Analysis/Merge Sort · Algorithm Analysis/Randomized Quick Sort
Algorithms/Search · Binary Search · Binary Search Modifications
Algorithms/Combinatorics · Algorithms/Combinatorics and Heuristics · Algorithms/Optimization · Divide and Conquer
Algorithms/Strings · Algorithm Analysis/Substring Pattern Matching
Algorithm complexity · Theta vs Big O Amortization · Amortization/Aggregate Method · Amortization/Accounting Method Algorithm Analysis/Matrix Multiplication
Estimation Estimation · Estimation/BitsAndBytes
Algorithm Practice and Writeups Project Euler · Five Letter Words · Letter Coverage
|