Insertion in array
Insertion in array
Insertion in array in C is Insertion operation is to insert one or more data elements into an array. Base on the requirement, a new element can be add at the beginning, end or any given index of array.
After inserting the element in the array, the positions or index location is increased but it does not mean the size of the array is increasing.
Diagramatic Representation:
Approach
- Consider the x which get the element to be insert
- secondly consider the pos word which get the position at which this element is to be insert
- Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos.
- Insert the element x now at the position pos, as this is now empty.
Algorithm of Insertion in array
Let LA be a Linear Array with N elements and K is a positive integer such that K<=N.Following is the algorithm where ITEM is inserted into the K position of LA
1.[Initialize Counter]
Set J=N
2.Repeat steps 3 and 4 while J>=K
3.Move jth element downward
Set LA[J+1]=LA[J]
4.Decrease Counter
Set J=J-1
End of step 2 loop
5.Insert element
Set LA[K]=ITEM
6.Reset N
Set N=N+1
7.Exit
Program in C
#include <stdio.h> int main() { int arr[100] = { 0 }; int i, x, pos, n = 10; for (i = 0; i < 10; i++) // initial array of size 10 arr[i] = i + 1; for (i = 0; i < n; i++) // print the original array printf("%d ", arr[i]); printf("\n"); x = 20; // element to be inserted pos = 5; // position at which element is to be inserted n++; for (i = n-1; i >= pos; i--) // shift elements forward arr[i] = arr[i - 1]; arr[pos - 1] = x; for (i = 0; i < n; i++) // print the updated array printf("%d ", arr[i]); printf("\n"); return 0; }
Output
1 2 3 4 5 6 7 8 9 10 1 2 3 4 20 5 6 7 8 9 10
Program in CPP
#include <iostream> using namespace std; int* insertX(int n, int arr[], int x, int pos) { int i; n++; for (i = n; i >= pos; i--) arr[i] = arr[i - 1]; arr[pos - 1] = x; return arr; } int main() { int arr[100] = { 0 }; int i, x, pos, n = 10; for (i = 0; i < 10; i++) arr[i] = i + 1; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; x = 20; pos = 5; insertX(n, arr, x, pos); for (i = 0; i < n + 1; i++) cout << arr[i] << " "; cout << endl; return 0; }
Output
1 2 3 4 5 6 7 8 9 10 1 2 3 4 20 5 6 7 8 9 10
Approach
- First consider the word “element” which to be inserted
- Consider the “position”which get the position at which this element is to be insert.
- Then Convert array to ArrayList
- Add element at position using list.add(position, element)
- Convert ArrayList back to array and print
Program in Java
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class AddElementAtPositionInArray { private static void addElement( // Method to add element at position Integer[] arr, int element, int position) { List<Integer> list = new ArrayList<>( // Converting array to ArrayList Arrays.asList(arr)); list.add(position - 1, element); // Adding the element at position arr = list.toArray(arr); // Adding the element at position System.out.println("Initial Array:\n" // Printing the original array + Arrays.toString(arr)); // Printing the updated array System.out.println("\nArray with " + element + " inserted at position " + position + ":\n" + Arrays.toString(arr)); } public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int element = 20; int position = 5; addElement(arr, element, position); } }
Output
Initial Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Array with 50 inserted at position 5: [1, 2, 3, 4, 20, 5, 6, 7, 8, 9, 10]
Time complexity
O(1)
Also, read the Array operation