0
0
DSA Cprogramming~10 mins

Generate All Permutations of Array in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Generate All Permutations of Array
Start with full array
Choose element at index i
Swap element i with element j (j >= i)
Recurse with i+1
If i == array length - 1
Print current permutation
Backtrack: Swap back elements i and j
Back to swap step for next j
We pick each element in turn, swap it with the current position, recurse to fix the next position, print when done, then swap back to restore.
Execution Sample
DSA C
void permute(int *arr, int start, int end) {
  if (start == end) {
    print(arr, end + 1);
  } else {
    for (int i = start; i <= end; i++) {
      swap(arr[start], arr[i]);
      permute(arr, start+1, end);
      swap(arr[start], arr[i]);
    }
  }
}
This code generates all permutations by swapping elements starting from 'start' index and recursing.
Execution Table
StepOperationIndices SwappedCurrent Array StateAction
1Start permute-[1, 2, 3]Call permute(arr, 0, 2)
2Swap(0,0)[1, 2, 3]Swap elements at 0 and 0
3Recurse-[1, 2, 3]Call permute(arr, 1, 2)
4Swap(1,1)[1, 2, 3]Swap elements at 1 and 1
5Recurse-[1, 2, 3]Call permute(arr, 2, 2)
6Print-[1, 2, 3]Print permutation
7Backtrack(1,1)[1, 2, 3]Swap back elements at 1 and 1
8Swap(1,2)[1, 3, 2]Swap elements at 1 and 2
9Recurse-[1, 3, 2]Call permute(arr, 2, 2)
10Print-[1, 3, 2]Print permutation
11Backtrack(1,2)[1, 2, 3]Swap back elements at 1 and 2
12Backtrack(0,0)[1, 2, 3]Swap back elements at 0 and 0
13Swap(0,1)[2, 1, 3]Swap elements at 0 and 1
14Recurse-[2, 1, 3]Call permute(arr, 1, 2)
15Swap(1,1)[2, 1, 3]Swap elements at 1 and 1
16Recurse-[2, 1, 3]Call permute(arr, 2, 2)
17Print-[2, 1, 3]Print permutation
18Backtrack(1,1)[2, 1, 3]Swap back elements at 1 and 1
19Swap(1,2)[2, 3, 1]Swap elements at 1 and 2
20Recurse-[2, 3, 1]Call permute(arr, 2, 2)
21Print-[2, 3, 1]Print permutation
22Backtrack(1,2)[2, 1, 3]Swap back elements at 1 and 2
23Backtrack(0,1)[1, 2, 3]Swap back elements at 0 and 1
24Swap(0,2)[3, 2, 1]Swap elements at 0 and 2
25Recurse-[3, 2, 1]Call permute(arr, 1, 2)
26Swap(1,1)[3, 2, 1]Swap elements at 1 and 1
27Recurse-[3, 2, 1]Call permute(arr, 2, 2)
28Print-[3, 2, 1]Print permutation
29Backtrack(1,1)[3, 2, 1]Swap back elements at 1 and 1
30Swap(1,2)[3, 1, 2]Swap elements at 1 and 2
31Recurse-[3, 1, 2]Call permute(arr, 2, 2)
32Print-[3, 1, 2]Print permutation
33Backtrack(1,2)[3, 2, 1]Swap back elements at 1 and 2
34Backtrack(0,2)[1, 2, 3]Swap back elements at 0 and 2
35End-[1, 2, 3]All permutations generated
💡 All swaps and recursions completed, original array restored
Variable Tracker
VariableStartAfter Step 2After Step 8After Step 13After Step 19After Step 24After Step 30Final
arr[1,2,3][1,2,3][1,3,2][2,1,3][2,3,1][3,2,1][3,1,2][1,2,3]
start0111111-
end2222222-
Key Moments - 3 Insights
Why do we swap back the elements after recursion?
Swapping back restores the array to its original state before the next iteration, as shown in steps like 7, 11, 22, 23, 33, and 34 in the execution_table.
Why do we recurse only when start < end?
When start == end (step 5, 9, 16, 20, 27, 31), we have fixed all positions and print the permutation. No further recursion is needed.
How does swapping elements generate all permutations?
By swapping each element into the current 'start' position and recursing, we explore all possible orders. The execution_table shows different swaps at steps 2, 8, 13, 19, 24, and 30.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is the array state?
A[1, 2, 3]
B[2, 1, 3]
C[1, 3, 2]
D[3, 2, 1]
💡 Hint
Check the 'Current Array State' column at step 8 in execution_table
At which step does the first permutation get printed?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'Print' operation in execution_table
If we skip swapping back after recursion, what happens to the array state?
AArray stays changed, permutations will be incorrect
BArray resets automatically, no effect
COnly first permutation prints correctly
DProgram crashes
💡 Hint
Refer to key_moments about why swapping back is needed and steps like 7 and 11
Concept Snapshot
Generate All Permutations of Array:
- Use recursion with a 'start' index
- Swap elements at 'start' with each index >= start
- Recurse with start+1
- Print when start == end
- Swap back after recursion to restore array
- This explores all possible orders of elements
Full Transcript
This concept shows how to generate all permutations of an array by swapping elements recursively. We start from the first index, swap it with itself and others, then recurse to fix the next index. When we reach the last index, we print the current array as a permutation. After each recursion, we swap back to restore the array for the next iteration. The execution table traces each swap, recursion, print, and backtrack step, showing how the array changes. Key moments clarify why swapping back is necessary and how recursion stops when the start index reaches the end. The visual quiz tests understanding of array states and recursion steps. This method ensures all permutations are generated without duplicates or missing any order.