Bird
0
0
DSA Cprogramming

Array Insertion at Beginning in DSA C

Choose your learning style9 modes available
Mental Model
To insert a new item at the start of an array, we move all existing items one step to the right to make space.
Analogy: Imagine a row of chairs where you want to add a new person at the front. Everyone must stand up and move one chair to the right to free the first chair.
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Insert new value 0 at index 0
After shift:
       [ ]->[1]->[2]->[3]->[4]->[5]
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], insert value 0 at beginning
Goal: Insert 0 at index 0 and shift all elements right
Step 1: Shift element at index 4 (5) to index 5
[1]->[2]->[3]->[4]->[5]->[5]
Why: Make space at the end to avoid overwriting data
Step 2: Shift element at index 3 (4) to index 4
[1]->[2]->[3]->[4]->[4]->[5]
Why: Continue shifting elements right to free index 0
Step 3: Shift element at index 2 (3) to index 3
[1]->[2]->[3]->[3]->[4]->[5]
Why: Keep shifting elements right
Step 4: Shift element at index 1 (2) to index 2
[1]->[2]->[2]->[3]->[4]->[5]
Why: Keep shifting elements right
Step 5: Shift element at index 0 (1) to index 1
[1]->[1]->[2]->[3]->[4]->[5]
Why: Last shift to free index 0
Step 6: Insert new value 0 at index 0
[0]->[1]->[2]->[3]->[4]->[5]
Why: Place new element at the beginning
Result:
[0]->[1]->[2]->[3]->[4]->[5]
Annotated Code
DSA C
#include <stdio.h>

void insertAtBeginning(int arr[], int *size, int capacity, int value) {
    if (*size >= capacity) {
        printf("Array is full, cannot insert\n");
        return;
    }
    // Shift elements right to make space at index 0
    for (int i = *size - 1; i >= 0; i--) {
        arr[i + 1] = arr[i]; // move element one step right
    }
    arr[0] = value; // insert new value at beginning
    (*size)++; // increase size
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i != size - 1) printf(" -> ");
    }
    printf("\n");
}

int main() {
    int capacity = 10;
    int arr[10] = {1, 2, 3, 4, 5};
    int size = 5;

    insertAtBeginning(arr, &size, capacity, 0);
    printArray(arr, size);

    return 0;
}
for (int i = *size - 1; i >= 0; i--) { arr[i + 1] = arr[i]; }
Shift all elements one position right to free index 0
arr[0] = value;
Insert new value at the beginning of the array
(*size)++;
Increase the size of the array after insertion
OutputSuccess
0 -> 1 -> 2 -> 3 -> 4 -> 5
Complexity Analysis
Time: O(n) because we must shift all existing elements one step to the right
Space: O(1) because insertion is done in place without extra arrays
vs Alternative: Compared to inserting at the end (O(1)), inserting at beginning requires shifting all elements, making it slower for large arrays
Edge Cases
Empty array
Insertion simply places the new value at index 0 without shifting
DSA C
for (int i = *size - 1; i >= 0; i--) { arr[i + 1] = arr[i]; }
Array at full capacity
Insertion is rejected with a message to avoid overflow
DSA C
if (*size >= capacity) { printf("Array is full, cannot insert\n"); return; }
Single element array
The single element is shifted right and new value placed at index 0
DSA C
for (int i = *size - 1; i >= 0; i--) { arr[i + 1] = arr[i]; }
When to Use This Pattern
When you need to add an element at the start of a fixed-size list and must keep order, use array insertion with shifting to make space.
Common Mistakes
Mistake: Shifting elements from left to right causing overwriting data
Fix: Shift elements from right to left (end to start) to avoid overwriting
Mistake: Not checking if array has capacity before insertion
Fix: Add a capacity check before shifting and inserting
Summary
Inserts a new element at the start of an array by shifting existing elements right.
Use when you want to add an item at the beginning of a fixed-size array while preserving order.
Shift elements from the end towards the start to avoid overwriting data during insertion.