We are given a sequence of numbers from 1 to n. Each permutation in the sequence that we need to generate should differ from the previous permutation by swapping just two adjacent elements of the sequence.
Input : n = 3 Output : 123 132 312 321 231 213 Input : n = 4 Output : 1234 1243 1423 4123 4132 1432 1342 1324 3124 3142 3412 4312 4321 3421 3241 3214 2314 2341 2431 4231 4213 2413 2143 2134
A simple solution to use permutations of n-1 elements to generate permutations of n elements.
For example let us see how to generate permutations of size 3 using permutations of size 2.
Permutations of two elements are 1 2 and 2 1.
Permutations of three elements can be obtained by inserting 3 at different positions in all permutations of size 2.
Inserting 3 in different positions of 1 2 leads to 1 2 3, 1 3 2 and 3 1 2.
Inserting 3 in different positions of 2 1 leads to 2 1 3, 2 3 1 and 3 2 1.
To generate permutations of size four, we consider all above six permutations of size three and insert 4 at different positions in every permutation.
Johnson and Trotter algorithm
The Johnson and Trotter algorithm doesn’t require to store all permutations of size n-1 and doesn’t require going through all shorter permutations. Instead, it keeps track of the direction of each element of the permutation.
- Find out the largest mobile integer in a particular sequence. A directed integer is said to be mobile if it is greater than its immediate neighbor in the direction it is looking at.
- Switch this mobile integer and the adjacent integer to which its direction points.
- Switch the direction of all the elements whose value is greater than the mobile integer value.
- Repeat the step 1 until unless there is no mobile integer left in the sequence.
Let us see how to generate permutations of size four. We first print 1 2 3 4. Initially all directions are right to left. <1 <2 <3 <4 All mobile numbers are 2, 3 and 4. The largest mobile number is 4. We swap 3 and 4. Since 4 is largest, we don't change any direction. <1 <2 <4 <3 4 is the largest mobile. We swap it with 2. <1 <4 <2 <3 4 is the largest mobile. We swap it with 1. <4 <1 <2 <3 3 is largest mobile. We swap it with 2 and change directions of greater elements. 4> <1 <3 <2 4 is largest mobile. We swap it with 3. <4 1> <2 <3 We proceed this way and print all permutations.
Below is the implementation of the approach.
1234 1243 1423 4123 4132 1432 1342 1324 3124 3142 3412 4312 4321 3421 3241 3214 2314 2341 2431 4231 4213 2413 2143 2134
- Heap's Algorithm for generating permutations
- Alexander Bogomolny’s UnOrdered Permutation Algorithm
- Total number of Subsets of size at most K
- Total number of BSTs using array elements
- Find sum of f(s) for all the chosen sets from the given array
- Count non decreasing subarrays of size N from N Natural numbers
- Number of pairs of arrays (A, B) such that A is ascending, B is descending and A[i] ≤ B[i]
- Number of binary strings such that there is no substring of length ≥ 3
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Number of ways to convert a character X to a string Y
- Expected number of moves to reach the end of a board | Dynamic programming
- Find the sum of the costs of all possible arrangements of the cells
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.