Print all permutation in lexicographic order
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Permutations are a fundamental concept in combinatorics and computer science, particularly in problems involving arrangements, ordering, and scheduling. Generating permutations in lexicographic order refers to creating permutations that are ordered based on the traditional alphabetical order (dictionary order) of their components. This approach is particularly useful when you need to systematically explore all possible configurations of a set, such as in backtracking algorithms or generating test cases.
Technical Explanation
Lexicographic Order
Lexicographic order generalizes the way words are alphabetically ordered in dictionaries. In this context, given two permutations A
and B
, A
is considered less than B
if during a left-to-right comparison, the first position where they differ has a lower value in A
than in B
.
Generating Permutations
The task of generating permutations in lexicographic order can be elegantly solved by using the algorithm that leverages the next-lexicographical permutation approach:
- Identify the longest non-increasing suffix: The rightmost element that is smaller than the element immediately after it denotes a pivot point.
- Find the successor to the identified pivot: This involves locating the smallest element in the suffix that is larger than the pivot and swapping it.
- Reverse the suffix: Following the swap, reverse the entire suffix beyond the pivot to achieve the next permutation in lexicographic order.
Example
Consider the sequence [1, 2, 3]
:
- Start with
[1, 2, 3]. - Identify the longest non-increasing suffix:
[3]. The pivot is2. - Find the smallest element in the suffix larger than
2, which is3, and swap them to get[1, 3, 2]. - Reverse the suffix starting immediately after the pivot to keep the suffix as low as possible, although in this case, since it’s a single element, no reversal is needed.
- Continue this process for each next permutation:
[2, 1, 3][2, 3, 1][3, 1, 2][3, 2, 1]
Python Implementation
Here's a simple Python implementation of the above algorithm:
- Avoid recalculating permutations from scratch; instead, generate the next permutation using in-place swaps.
- Use iterative approaches instead of recursion to prevent stack overflow in deeper permutations.
- Backtracking Algorithms: Exploring all configurations systematically can help in constraint satisfaction problems.
- Testing and Validation: Permutations allow comprehensive coverage of input scenarios.
- Cryptographic Variants: Generating all permutations can be used to analyze symmetric encryption schemes.
Related reading
- Print all unique combination of factors of a given number
- Print binary tree in BFS fashion with O1 space
- Print Specific nodes at a every level calculated by a given function
- Print two-dimensional array in spiral order
- Printing all possible subsets of a list
- Probability and Neural Networks
- Printing all possible words from a 2D array of characters
- Printing BFS Binary Tree in Level Order with Specific Formatting

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.