Open In App

Inserting Elements in an Array | Array Operations

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this post, we will look into insertion operation in an Array, i.e., how to insert into an Array, such as:

  1. Inserting Elements in an Array at the End
  2. Inserting Elements in an Array at any Position in the Middle
  3. Inserting Elements in a Sorted Array

1. Inserting Elements in an Array at the End:

In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to care about the position at which the element is to be placed.

Coding implementation of inserting an element at the end:

C++
#include<bits/stdc++.h>
// Include the C++ input/output library 

using namespace std; 
//Using the standard namespace for convenience 

// Function to insert a key into an array 
int insertSorted(int arr[], int n, int key, int capacity) { 
  
  // Check if the array is already full 
  if (n >= capacity) 
    return n; 
  
  // Add the key at the end of the array
  arr[n] = key; 
 
  // Return the updated size of the array
    return (n + 1); 
} 
int main() {
  int arr[20] = {12, 16, 20, 40, 50, 70}; 
  
  int capacity = sizeof(arr) / sizeof(arr[0]);
  
  int n = 6; 
  int i, key = 26;
  
  cout << "\n Before Insertion: "; 
  for (i = 0; i < n; i++) 
    cout << arr[i] << " "; // Inserting the key into the array 
  
  n = insertSorted(arr, n, key, capacity);
  
  cout << "\n After Insertion: "; 
  for (i = 0; i < n; i++) 
    cout << arr[i] << " "; 
  
  return 0;
  
  //This code is contributed by Dhruv kumar
}
C Java Python3 C# Javascript PHP

Output
 Before Insertion: 12 16 20 40 50 70 
 After Insertion: 12 16 20 40 50 70 26 

Time Complexity: O(n)

Auxiliary Space: O(1)

2. Inserting Elements in an Array at any Position:

Insert operation in an array at any position can be performed by shifting elements to the right, which are on the right side of the required position

Coding implementation of inserting an element at any position:

C++
// C++ Program to Insert an element
// at a specific position in an Array

#include <bits/stdc++.h>
using namespace std;

// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
    // shift elements to the right
    // which are on the right side of pos
    for (int i = n - 1; i >= pos; i--)
        arr[i + 1] = arr[i];

    arr[pos] = x;
}

// Driver's code
int main()
{
    int arr[15] = { 2, 4, 1, 8, 5 };
    int n = 5;

    cout << "Before insertion : ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    cout << endl;

    int x = 10, pos = 2;

    // Function call
    insertElement(arr, n, x, pos);
    n++;

    cout << "After insertion : ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
}
C Java Python3 C# Javascript PHP

Output
Before insertion : 2 4 1 8 5 
After insertion : 2 4 10 1 8 5 

Time complexity: O(N)
Auxiliary Space: O(1)

3. Inserting Elements in a Sorted Array:

In a sorted array, a search operation is performed for the possible position of the given element by using Binary search, and then an insert operation is performed followed by shifting the elements. And in an unsorted array, the insert operation is faster as compared to the sorted array because we don’t have to care about the position at which the element is placed.

Insert Operation in sorted array

Below is the implementation of the above approach:

C
// C program to implement insert operation in
// an sorted array.
#include <stdio.h>

// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
    // Cannot insert more elements if n is already
    // more than or equal to capacity
    if (n >= capacity)
        return n;

    int i;
    for (i = n - 1; (i >= 0 && arr[i] > key); i--)
        arr[i + 1] = arr[i];

    arr[i + 1] = key;

    return (n + 1);
}

/* Driver code */
int main()
{
    int arr[20] = { 12, 16, 20, 40, 50, 70 };
    int capacity = sizeof(arr) / sizeof(arr[0]);
    int n = 6;
    int i, key = 26;

    printf("\nBefore Insertion: ");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);

    // Function call
    n = insertSorted(arr, n, key, capacity);

    printf("\nAfter Insertion: ");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}
Java Python3 C# Javascript C++

Output
Before Insertion: 12 16 20 40 50 70 
After Insertion: 12 16 20 26 40 50 70 

Time Complexity: O(N) [In the worst case all elements may have to be moved] 
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads