Bird
0
0
DSA Cprogramming~10 mins

Array Insertion at Beginning in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Array Insertion at Beginning
Check if array is full
No
Shift elements right by one
Insert new element at index 0
Increase array size
Done
To insert at the beginning, first shift all elements right, then place the new element at index 0, and update the size.
Execution Sample
DSA C
int arr[5] = {10, 20, 30};
int size = 3;
int capacity = 5;
int new_element = 5;

// Insert at beginning
if (size < capacity) {
  for (int i = size; i > 0; i--) arr[i] = arr[i-1];
  arr[0] = new_element;
  size++;
}
This code inserts 5 at the beginning of array {10, 20, 30} shifting elements right.
Execution Table
StepOperationArray StatePointer ChangesVisual State
1Check if size < capacity10, 20, 30, _, _size=3, capacity=5Array has space, proceed
2Shift element at index 3 (empty) to index 410, 20, 30, _, _arr[4] = arr[3]10 -> 20 -> 30 -> _ -> _
3Shift element at index 2 to index 310, 20, 30, 30, _arr[3] = arr[2]10 -> 20 -> 30 -> 30 -> _
4Shift element at index 1 to index 210, 20, 20, 30, _arr[2] = arr[1]10 -> 20 -> 20 -> 30 -> _
5Shift element at index 0 to index 110, 10, 20, 30, _arr[1] = arr[0]10 -> 10 -> 20 -> 30 -> _
6Insert new element 5 at index 05, 10, 20, 30, _arr[0] = 55 -> 10 -> 20 -> 30 -> _
7Increase size by 15, 10, 20, 30, _size = 45 -> 10 -> 20 -> 30 -> _
8End5, 10, 20, 30, _size=4Insertion complete
💡 Insertion stops after size reaches 4, array updated with new element at start.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
arr[10,20,30,_,_][10,20,30,_,_][10,20,30,30,_][10,20,20,30,_][10,10,20,30,_][5,10,20,30,_][5,10,20,30,_]
size3333334
capacity5555555
Key Moments - 3 Insights
Why do we shift elements starting from the end and not from the beginning?
Shifting from the end prevents overwriting elements before they are moved, as shown in steps 2 to 5 in the execution_table.
What happens if the array is already full?
The insertion does not proceed because size is not less than capacity, as checked in step 1.
Why do we increase the size only after inserting the new element?
Increasing size after insertion ensures the new element is counted in the array length, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 4?
A10, 10, 20, 30, _
B10, 20, 20, 30, _
C5, 10, 20, 30, _
D10, 20, 30, _, _
💡 Hint
Check the 'Array State' column for step 4 in execution_table.
At which step does the new element get inserted at index 0?
AStep 6
BStep 5
CStep 7
DStep 2
💡 Hint
Look for the operation 'Insert new element 5 at index 0' in execution_table.
If the array size was already equal to capacity at start, what would happen?
AInsertion proceeds normally
BArray elements shift but no insertion
CInsertion does not happen at all
DSize increases beyond capacity
💡 Hint
Refer to step 1 where size < capacity is checked.
Concept Snapshot
Array Insertion at Beginning:
- Check if array has space (size < capacity)
- Shift all elements right by one (from end to start)
- Insert new element at index 0
- Increase size by 1
- Stops if array is full
Full Transcript
To insert an element at the beginning of an array, first check if there is space. If yes, shift all existing elements one position to the right starting from the last element to avoid overwriting. Then place the new element at index 0 and increase the size count. If the array is full, insertion does not happen. This process ensures the new element is at the start and the order of other elements is preserved.