Bird
0
0
DSA Cprogramming~10 mins

Next Permutation of Array in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Next Permutation of Array
Start from right end
Find first pair where nums[i
If no such pair, reverse whole array -> Done
Yes
Find element nums[j
Swap nums[i
Reverse subarray from i+1 to end
Done with next permutation
Find the next lexicographically greater permutation by locating a pivot, swapping, and reversing the suffix.
Execution Sample
DSA C
int i = n - 2;
while (i >= 0 && nums[i] >= nums[i+1]) i--;
if (i >= 0) {
  int j = n - 1;
  while (nums[j] <= nums[i]) j--;
  swap(nums[i], nums[j]);
}
reverse(nums + i + 1, nums + n);
This code finds the next permutation of the array nums in-place.
Execution Table
StepOperationijArray StateNotes
1Initialize i = n-24-[1, 2, 3, 6, 5, 4]Start from second last index
2Check nums[i] >= nums[i+1]4-[1, 2, 3, 6, 5, 4]5 >= 4 is True, decrement i
3Check nums[i] >= nums[i+1]3-[1, 2, 3, 6, 5, 4]6 >= 5 is True, decrement i
4Check nums[i] >= nums[i+1]2-[1, 2, 3, 6, 5, 4]3 >= 6 is False, stop
5Initialize j = n-125[1, 2, 3, 6, 5, 4]j starts at last index
6Check nums[j] <= nums[i]25[1, 2, 3, 6, 5, 4]4 <= 3 is False, stop
7Swap nums[i] and nums[j]25[1, 2, 4, 6, 5, 3]Swap 3 and 4
8Reverse subarray from i+1 to end25[1, 2, 4, 3, 5, 6]Reverse [6,5,3] to [3,5,6]
9Done--[1, 2, 4, 3, 5, 6]Next permutation found
💡 After reversing suffix, next permutation is complete.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7Final
i43222-
j---55-
Array[1, 2, 3, 6, 5, 4][1, 2, 3, 6, 5, 4][1, 2, 3, 6, 5, 4][1, 2, 3, 6, 5, 4][1, 2, 4, 6, 5, 3][1, 2, 4, 3, 5, 6]
Key Moments - 3 Insights
Why do we start searching for i from the right end (n-2) and not from the left?
Because the next permutation changes the suffix to the smallest bigger arrangement. Starting from right finds the longest non-increasing suffix quickly (see steps 1-4 in execution_table).
What if no i is found such that nums[i] < nums[i+1]?
It means the array is in descending order, the highest permutation. We reverse the whole array to get the lowest permutation (not shown in this example).
Why do we reverse the subarray after swapping nums[i] and nums[j]?
Because the suffix after i was in descending order. Reversing it makes it ascending, which is the smallest order to get the next permutation (see step 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of i?
A3
B1
C2
D4
💡 Hint
Check the 'i' column in execution_table row with Step '4'
At which step does the array first change from the original?
AStep 6
BStep 7
CStep 8
DStep 5
💡 Hint
Look for the first step where 'Array State' differs from original in execution_table
If the array was [3, 2, 1], what would happen according to the concept_flow?
ANo i found, reverse whole array
BOnly swap first two elements
CFind i and j, swap, then reverse suffix
DNo changes, array is already next permutation
💡 Hint
Refer to concept_flow step 'If no such pair, reverse whole array -> Done'
Concept Snapshot
Next Permutation Algorithm:
- Start from right, find first i where nums[i] < nums[i+1]
- If none, reverse entire array (lowest permutation)
- Else find j from right where nums[j] > nums[i]
- Swap nums[i] and nums[j]
- Reverse subarray from i+1 to end
- Result is next lexicographically greater permutation
Full Transcript
The next permutation algorithm finds the next bigger arrangement of numbers by scanning from the right to find a pivot where the order breaks ascending. If none found, the array is reversed to the smallest order. Otherwise, it swaps the pivot with the next bigger number on the right and reverses the suffix to get the next permutation. This process is done in-place and ensures the next lexicographical order.