# Algorithm For Insertion In Array

• Insertion in the operation in which a new value is added at a particular place in an array. • 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'

D is inserted and element after Reg[4] are shifted to one place.

## 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.

let Reg[10] an array with holding capacity of 10 element but it has only 7 elements in it.

so N=10, M=7, i=4

step 1 if(M<N) then Back=M+1 else stop
7<10 true Back =7+1
2. (While(Back>i)repeat to 4 )
3. Reg[8]=Reg[8-1]=> Reg[8]='G'
4. Back =8-1=7
when Back=4 then
Back>i=>4>4 false
5. Reg[4]='x'
6. M=M+1 (after insertion the last element in Reg is at (M+1)th location)
7. End.

```