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:
#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 program to implement insert
// operation in an unsorted 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;
arr[n] = 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("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
printf("\n After Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
// Java program to implement insert
// operation in an unsorted array.
class Main {
// Function to insert a given key in
// the array. This function returns n+1
// if insertion is successful, else n.
static 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;
arr[n] = key;
return (n + 1);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
System.out.print("Before Insertion: ");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Inserting key
n = insertSorted(arr, n, key, capacity);
System.out.print("\nAfter Insertion: ");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
# Python program for inserting
# an element in an unsorted array
# method to insert element
def insert(arr, element):
arr.append(element)
# Driver's code
if __name__ == '__main__':
# declaring array and key to insert
arr = [12, 16, 20, 40, 50, 70]
key = 26
# array before inserting an element
print("Before Inserting: ")
print(arr)
# array after Inserting element
insert(arr, key)
print("After Inserting: ")
print(arr)
# Thanks to Aditi Sharma for contributing
# this code
// C# program to implement insert
// operation in an unsorted array.
using System;
class main {
// Function to insert a given
// key in the array. This
// function returns n + 1
// if insertion is successful,
// else n.
static 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;
arr[n] = key;
return (n + 1);
}
// Driver Code
public static void Main()
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
Console.Write("Before Insertion: ");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
// Inserting key
n = insertSorted(arr, n, key, capacity);
Console.Write("After Insertion: ");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by vt_m.
// Javascript program to implement insert
// operation in an unsorted array.
// Function to insert a given
// key in the array. This
// function returns n + 1
// if insertion is successful,
// else n.
function insertSorted(arr, n, key, capacity)
{
// Cannot insert more elements
// if n is already more than
// or equal to capacity
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
let arr = new Array(20);
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
let capacity = 20;
let n = 6;
let i, key = 26;
console.log("Before Insertion: ");
for (i = 0; i < n; i++)
console.log(arr[i]+" ");
console.log("</br>");
// Inserting key
n = insertSorted(arr, n, key, capacity);
console.log("After Insertion: ");
for (i = 0; i < n; i++)
console.log(arr[i]+" ");
<?php
// PHP program to implement insert
// operation in an unsorted array.
// 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.
function insertSorted(&$arr, $n, $key,
$capacity)
{
// Cannot insert more elements if n is
// already more than or equal to capacity
if ($n >= $capacity)
return $n;
array_push($arr, $key);
return ($n + 1);
}
// Driver Code
$arr = array(12, 16, 20, 40, 50, 70);
$capacity = 20;
$n = 6;
$key = 26;
echo "Before Insertion: ";
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
// Inserting key
$n = insertSorted($arr, $n,
$key, $capacity);
echo "\nAfter Insertion: ";
for ($i = 0; $i < $n; $i++)
echo $arr[$i] . " ";
// This code is contributed by
// Rajput-Ji
?>
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)
Inserting Elements in an Array | Array Operations
In this post, we will look into insertion operation in an Array, i.e., how to insert into an Array, such as:
- Inserting Elements in an Array at the End
- Inserting Elements in an Array at any Position in the Middle
- Inserting Elements in a Sorted Array
Contact Us