Insert an Element into an Array in C – At Position and Sorted

Inserting an element into an array means placing a new value at a specific position while shifting existing elements to make room. Because C arrays are fixed in size, you need to allocate enough space upfront and track the current number of elements separately. This guide shows two practical approaches: inserting at a given index and inserting into a sorted array to keep it ordered.

How Array Insertion Works

The key idea: to insert at position pos, shift every element from pos onward one step to the right, then write the new value into arr[pos]. You must shift from the rightmost element backward to avoid overwriting data.

Before: 10 20 30 40 50
Insert 25 at position 2:
  arr[5] = arr[4]  →  10 20 30 40 50 50
  arr[4] = arr[3]  →  10 20 30 40 40 50
  arr[3] = arr[2]  →  10 20 30 30 40 50
  arr[2] = 25      →  10 20 25 30 40 50

Program 1 – Insert at a Specific Position

#include <stdio.h>

void print_array(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int main(void)
{
    int arr[10] = {10, 20, 30, 40, 50};
    int n = 5;
    int pos = 2;
    int val = 25;
    int i;

    printf("Before: ");
    print_array(arr, n);

    for (i = n; i > pos; i--)
        arr[i] = arr[i - 1];
    arr[pos] = val;
    n++;

    printf("After inserting %d at position %d: ", val, pos);
    print_array(arr, n);
    return 0;
}

Output

Before: 10 20 30 40 50
After inserting 25 at position 2: 10 20 25 30 40 50 

How It Works

  • arr[10] — declared with capacity 10 so there is room for one more element
  • The loop runs from i = n down to i = pos + 1, copying each element one slot to the right
  • After shifting, arr[pos] is free to receive the new value
  • n++ updates the logical size so the array is printed correctly

Program 2 – Insert into a Sorted Array

When the array is already sorted, the insertion position is not given — you find it by scanning for the first element larger than the value being inserted.

#include <stdio.h>

void print_array(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int insert_sorted(int arr[], int n, int val)
{
    int pos = n;
    int i;
    for (i = 0; i < n; i++) {
        if (val < arr[i]) { pos = i; break; }
    }
    for (i = n; i > pos; i--)
        arr[i] = arr[i - 1];
    arr[pos] = val;
    return n + 1;
}

int main(void)
{
    int arr[10] = {10, 20, 40, 60, 80};
    int n = 5;

    printf("Original:         ");
    print_array(arr, n);

    n = insert_sorted(arr, n, 35);
    printf("After insert 35:  ");
    print_array(arr, n);

    n = insert_sorted(arr, n, 5);
    printf("After insert 5:   ");
    print_array(arr, n);

    n = insert_sorted(arr, n, 90);
    printf("After insert 90:  ");
    print_array(arr, n);

    return 0;
}

Output

Original:         10 20 40 60 80
After insert 35:  10 20 35 40 60 80
After insert 5:   5 10 20 35 40 60 80
After insert 90:  5 10 20 35 40 60 80 90

How It Works

  • The first loop finds pos — the index of the first element greater than val. If no such element exists, pos stays at n (append at end).
  • The second loop is identical to Program 1 — shift right from the end down to pos.
  • The function returns the new size so the caller always has the correct count.
  • Inserting 5 (less than all) correctly places it at index 0; inserting 90 (greater than all) appends it — both edge cases handled.

How to Compile and Run

gcc -ansi -Wall -Wextra insert1.c -o insert1
./insert1

Time and Space Complexity

Operation Time Space
Insert at position (best — end) O(1) O(1)
Insert at position (worst — front) O(n) O(1)
Insert into sorted array O(n) O(1)

The shift loop is the bottleneck. Inserting at the front shifts every element — O(n). Appending at the end shifts nothing — O(1). Average case is O(n/2) = O(n).

Common Mistakes

Mistake Effect Fix
Array not large enough Buffer overflow, undefined behavior Declare capacity ≥ max expected size
Shifting left-to-right instead of right-to-left Overwrites elements before copying them Loop from i = n down to pos + 1
Forgetting to increment n Last inserted element is ignored in future operations Always do n++ or return n + 1
Using void main() or conio.h Non-standard, won’t compile with strict flags Use int main(void), ANSI headers only

Related Programs

Recommended Books

These are the two C books I recommend for mastering arrays and pointers:

Practice C with the C Programming Quiz App — 500+ MCQs covering arrays, pointers, and more.
Download on Google Play →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>