# Algorithm to Insert and Delete Element – Array Operation

Algorithm to Insert Element in Array

Insert Operation on Array and Delete Operation on Array is part of Learning Data Structure Series. In the previous article of Array Data Structure we learnt about arrays and different types of array data structures.

In this article we will start with Insert Operation on Array where we will learn the Algorithm to Insert element in array. Later part of the article is based on Delete Operation on Array where we will learn the Algorithm to Delete element from array.

## Insert Operation on Array (Algorithm to Insert Element in Array)

The term insert operation on array refers to adding element to an array at a particular location. We can insert element in array at any location. Algorithm to insert element in array requires the specific location at which the element is to be added.

Algorithm to Insert Element in Array is as follows:

INSERT(DATA, N, I, ELEMENT, LOC)

```1. Set I:=N
2. Repeat the step 3 and 4 while I>=LOC
3. Set DATA[I+1]:=DATA[I]
4. I=I-1
[End of loop]
5. Set DATA[I]=ELEMENT
6. N=N+1
7. Exit
```

This algorithm performs Insert Operation on Array; it adds an ELEMENT at the given location LOC in the array – DATA. In the first step we initialized variable I with the maximum number of element present in the array (I:=N). After that with loop we shift the element forward till we reach the location LOC. In the next step, we insert the ELEMENT at its location.

Example: Consider an array Data[10] having 6 elements in it.

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 2 35 60 45

Now we want to place 100 at location 4. Algorithm to Insert Element in Array starts with shifting the elements to right as given in the figures below

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 2 35 60 45 ↪

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 2 35 60 45 ↪

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 2 35 60 45 ↪

After Insertion

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 2 100 35 60 45

In this way, we learnt the way Algorithm to Insert Element in Array is executed by shifting the elements to the right. This is done to create a space for a new element and then placing the element at a given location.

### Shortcoming of Algorithm to Insert Element in Array

We saw that Insert Operation needs shifting of elements, from a given location to its right, in order to accommodate a new element. For a large array, if the desired location for insertion is near to its beginning, we will have to shift huge amount of data to the end of the array. This reduces the operation speed and consumes a considerable amount of resources.

Algorithm to Delete Element From Array

## Delete Operation on Array (Algorithm to Delete Element from Array)

Now we will learn to perform Delete Operation on Array. To delete element from array we should know the location from where we want to delete. Algorithm to Delete Element from Array can be executed at any location ie. at it beginning, in middle or at the end of the array.

Apply a loop from the location of item till the end of the array. Shift the element forward. In this algorithm, DATA is an array. ELEMENT is the value which we want to delete from the location LOC. N is the maximum number of element present in the array DATA.

Algorithm to Delete Element from Array is:

DELETE(DATA, N, I, ELEMENT, LOC)

```1.Set ELEMENT=DATA[LOC]
2.Repeat for I=LOC to N-1
Set DATA[I]:=DATA[I+1]
[End of loop]
3.N:=N-1
4.Exit.
```

The Algorithm to Delete Element from Array performs the delete operation at location – LOC.

Example: Let us consider an array – DATA.

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 15 20 35 60 45

Delete element 15 from location 2

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 20 35 60 45 ↩

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 20 35 60 45 ↩

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 20 35 60 45 ↩

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 20 35 60 45 ↩

After Deletion array will look like

 Index 1 2 3 4 5 6 7 8 9 10 Value 10 20 35 60 45

In this way by shifting elements to the left Algorithm to Delete Element from Array is executed on a particular location by overwriting it with the element on its right and continue to do so till the end of the array.

### Shortcoming of Algorithm to Delete Element from Array

We saw that Delete Operation needs shifting of elements, for a given location to its left, in order to replace the element to be removed. For a large array, if the desired location of deletion is near to its beginning, we will have to shift huge amount of data towards the beginning of the array. This reduces the operation speed and consumes a considerable amount of resources.

Due to the shortcomings of linear data structure to handle large Insert and Delete Operations it is not the most efficient way to store large amount of data.

Next article of Learning Data Structure series is on Search Operation where we will learn to find the location of an element with Linear Search Algorithm in Array.