Bird
0
0
DSA Cprogramming~10 mins

Array Reversal Techniques in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Array Reversal Techniques
Start with array
Set left = 0, right = length-1
Check if left < right?
NoDone
Yes
Swap elements at left and right
Increment left, Decrement right
Back to Check
The process swaps elements from the ends moving inward until the middle is reached.
Execution Sample
DSA C
int arr[] = {1, 2, 3, 4, 5};
int left = 0, right = 4;
while (left < right) {
  int temp = arr[left];
  arr[left] = arr[right];
  arr[right] = temp;
  left++;
  right--;
}
This code reverses the array by swapping elements from the start and end moving towards the center.
Execution Table
StepOperationleftrightArray StatePointer Changes
1Initialize left and right04[1, 2, 3, 4, 5]left=0, right=4
2Check left < right04[1, 2, 3, 4, 5]0 < 4 is True
3Swap arr[left] and arr[right]04[5, 2, 3, 4, 1]Swapped arr[0] and arr[4]
4Increment left, Decrement right13[5, 2, 3, 4, 1]left=1, right=3
5Check left < right13[5, 2, 3, 4, 1]1 < 3 is True
6Swap arr[left] and arr[right]13[5, 4, 3, 2, 1]Swapped arr[1] and arr[3]
7Increment left, Decrement right22[5, 4, 3, 2, 1]left=2, right=2
8Check left < right22[5, 4, 3, 2, 1]2 < 2 is False
9Done22[5, 4, 3, 2, 1]Reversal complete
💡 left is not less than right (2 < 2 is False), so loop ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7Final
left001122
right443322
arr[1, 2, 3, 4, 5][5, 2, 3, 4, 1][5, 2, 3, 4, 1][5, 4, 3, 2, 1][5, 4, 3, 2, 1][5, 4, 3, 2, 1]
Key Moments - 3 Insights
Why do we stop when left is not less than right?
Because when left equals or passes right, all pairs have been swapped. See execution_table rows 8 and 9 where 2 < 2 is False, so reversal is complete.
Why do we swap elements at left and right instead of just reversing by copying?
Swapping in place saves memory by not needing extra space. The execution_table shows how elements at positions left and right are exchanged step-by-step.
What happens if the array has an odd number of elements?
The middle element stays in place because left and right meet at the center. In the example, arr[2] = 3 remains unchanged as shown in the array state after step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the array state?
A[5, 4, 3, 2, 1]
B[5, 2, 3, 4, 1]
C[1, 2, 3, 4, 5]
D[1, 4, 3, 2, 5]
💡 Hint
Check the 'Array State' column at step 6 in the execution_table.
At which step does the condition left < right become false?
AStep 7
BStep 8
CStep 5
DStep 9
💡 Hint
Look at the 'Pointer Changes' and 'Operation' columns in execution_table rows 8 and 9.
If the array length was 6 instead of 5, how would the final left and right values differ?
Aleft=2, right=3
Bleft=2, right=2
Cleft=3, right=2
Dleft=3, right=3
💡 Hint
Consider how left increments and right decrements until left >= right for even length arrays.
Concept Snapshot
Array Reversal Technique:
- Use two pointers: left at start, right at end
- While left < right:
  - Swap arr[left] and arr[right]
  - Increment left, decrement right
- Stops when pointers meet or cross
- Works in-place without extra memory
Full Transcript
This visualization shows how to reverse an array by swapping elements from the ends moving inward. We start with two pointers, left at index 0 and right at the last index. While left is less than right, we swap the elements at these positions, then move left forward and right backward. This continues until left is no longer less than right, meaning the array is reversed. The example array [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1] after the process. Key points include stopping when pointers meet or cross, and that the middle element in odd-length arrays stays in place. This method reverses the array in-place without extra memory.