Insert an element AT SPECIFIED position in an array of size S

Insert an element at SPECIFIED position in an array of size S.

Algorithm:

1.       START

2.        N = Get Total number of elements currently present in the array (A)  (Assuming that elements are present without any empty spaces so if an array is

A{   23, 24, 25, <no element>, 26, <no element>  }, N returned by this function is 6)

3.       If N==0, goto 5

4.       If (N<S),  goto 5, else goto 10

5.       position = Get Position where element has to be inserted.

6.       If(position>size -1), goto 10

7.       Ele = Get a new element to insert

8.       If (N>0) shift all elements from A[position] to one position right

9.       A[pos] = Ele

10.   STOP

 

Implementation:

Scenario 1: Some elements are present.

Array A[10]. N=5. S=10, position=3

ARRAY BEFORE INSERTION:

15

20

26

78

25

 

 

 

 

 

 

1.       START

2.       N=5

3.       N is not zero so goto next step (5).

4.       5<10, so going to next step

5.       Position = 3

6.       3>9, which is false, so going to next step

7.       Ele = 13

8.       Move all elements from A[3] to one position right. After this step, array looks like:

15

20

26

 

78

25

 

 

 

 

 

9.        A[3] = 13

10.   STOP

 

ARRAY AFTER INSERTION:

15

20

26

13

78

25

 

 

 

 

 

 

 

Scenario 2: Array is EMPTY.

Array A[10]. N=0. S=10, position=3

ARRAY BEFORE INSERTION:

 

 

 

 

 

 

 

 

 

 

 

1.       START

2.       N=0

3.       Since N=0, going to STEP 5

4.       <NOT EXECUTED>

5.       Position = 3

6.       3>9 is false, going to next step

7.       Ele = 13

8.       0>0 is not true, so going to next step, without shifting

9.       A[3]=13

10.   STOP

ARRAY AFTER INSERTION

 

 

 

13

 

 

 

 

 

 

 

 

 

 

 

Scenario 3: Array is FULL

Array A[10]. N=10. S=10, position=3

ARRAY BEFORE INSERTION:

15

20

26

78

25

19

87

92

34

16

 

1.       START

2.       N=10

3.       N is not zero so going to next step.

4.       10<10, which is false hence goto 10

5.       <NOT EXECUTED>

6.       <NOT EXECUTED>

7.       <NOT EXECUTED>

8.       <NOT EXECUTED>

9.       <NOT EXECUTED>

10.   STOP

 

ARRAY AFTER INSERTION (NO CHANGE):

15

20

26

78

25

19

87

92

34

16

 

– submitted by myworld

 

Submit Content and Earn Money

About the Author

aditya bajaj
Music and Blogging get my love.

Be the first to comment on "Insert an element AT SPECIFIED position in an array of size S"

Leave a comment

Your email address will not be published.


*