State algorithm for inserting an element in an Array.
(1) Inserting element to an array is the process of adding an element to the existing elements of array.
(2) The element can be easily inserted at the end of an array. But for insertion in the middle of an array it is required to move the elements one byte forward.
(3) The following algorithm inserts a data element in an array:-
Algorithm for inserting element into a linear array
INSERT [LA, N, K, ITEM]
LA = Linear array
N = Total no. of elements in the array
K = Any positive integer, K<=N
This algorithm inserts an element ITEM into the Kth position in 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
|Number||9 is inserted||4 is deleted|
|Original Array||Addition of element in
|Deletion of element from array|