2080 10

BIT152 ยท 2080 ยท 5 marks

What is permutation? What is the next permutation in lexicographic order after 362541?

What is permutation? What is the next permutation in lexicographic order after 362541? [5]

Discrete StructureChapter 7: Combinatorics and CountingPermutationsNumerical: next lexicographic permutation

Permutation and Next Permutation in Lexicographic Order

Given Data

  • Current permutation: 362541 (digits: 3, 6, 2, 5, 4, 1)
  • Task: find the next permutation in lexicographic (dictionary) order.

What is a Permutation?

A permutation of a set of elements is an arrangement of those elements in a definite order. For a set of $n$ distinct elements, the total number of permutations is:

$$n! = n \times (n-1) \times (n-2) \times \cdots \times 1$$

Example: For ${1, 2, 3}$, the $3! = 6$ permutations are: $$123,\ 132,\ 213,\ 231,\ 312,\ 321$$

In lexicographic order, permutations are ordered like words in a dictionary, comparing element by element from left to right.


Next Permutation Algorithm

Step 1: Find the largest index $i$ such that $a[i] < a[i+1]$ (the pivot). Step 2: Find the largest index $j > i$ such that $a[j] > a[i]$. Step 3: Swap $a[i]$ and $a[j]$. Step 4: Reverse the suffix starting at $a[i+1]$.


Applying to 362541

Index the digits (0-based):

Index012345
Digit362541

Step 1: Find pivot (scan right to left):

  • $a[4]=4 > a[5]=1$ โ†’ no
  • $a[3]=5 > a[4]=4$ โ†’ no
  • $a[2]=2 < a[3]=5$ โ†’ pivot found, $i = 2$, value $= 2$

Step 2: Find successor $a[j] > 2$ (scan right to left):

  • $a[5]=1 < 2$ โ†’ skip
  • $a[4]=4 > 2$ โ†’ found, $j = 4$

Step 3: Swap $a[2]$ and $a[4]$:

Before: $3,\ 6,\ \mathbf{2},\ 5,\ \mathbf{4},\ 1$ After: $3,\ 6,\ \mathbf{4},\ 5,\ \mathbf{2},\ 1$

Step 4: Reverse suffix from index 3 to end:

Suffix $5, 2, 1 \Rightarrow 1, 2, 5$

Result: $3,\ 6,\ 4,\ 1,\ 2,\ 5$


Result

The next permutation in lexicographic order after 362541 is $\boxed{364125}$.

Verification: $364125 > 362541$ and it is the smallest such permutation (the change occurs at the rightmost possible position with the smallest valid increase), confirming correctness.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.