1. Insertion Operation in Array(DS)
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
2. Insertion Operation in Array
Insertion operation is used to insert a new element at
specific position in to one dimensional array.
In order to insert a new element into one dimensional
array we have to create space for new element.
3. Cont..
Suppose there are N elements in an array and we want
to insert a new element between first and second
element.
We have to move last N-1 elements down in order to
create space for the new element.
4. Algorithm For Insertion Operation in Array
Insertion in the operation in which a new value is
added at a particular place in an array.
5. Cont..
In this element D to be inserted at place 4th.
All the element after D has to be shifted.
Reg[9]=Reg[8] Reg[8]=Reg[7]
.
.
Reg[5]=Reg[4]
Reg[4]='D'
7. Algorithm For Insertion In An Array
Array Reg[N] with last element at Mith position value X
is to be inserted at ith location.
Steps:
• 1. If(M<N) then BACK=M+1 else STOP
• 2. While (BACK>i) repeat steps 3 to 4
• 3. REG[BACK]= REG[BACK-1]
• 4. BACK=BACK-1;
• 5. Reg[BACK]='X'
• 6. M=M+1
• 7. End.
8. Assignment
Explain insertion of element of specific position in
array. Discuss all the steps of algorithm with an
example.