0% found this document useful (0 votes)
16 views2 pages

Algorithm For Insert Element Into A Array

The document outlines algorithms for inserting and deleting elements in an array, detailing the steps involved in each process. It provides a visual representation with examples for clarity, such as inserting the element 80 at position 2 in an array of integers and removing the element 12. Both operations have a time complexity of O(n) in the worst case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Algorithm For Insert Element Into A Array

The document outlines algorithms for inserting and deleting elements in an array, detailing the steps involved in each process. It provides a visual representation with examples for clarity, such as inserting the element 80 at position 2 in an array of integers and removing the element 12. Both operations have a time complexity of O(n) in the worst case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm for inserting elements into an array:

Insert(array, N, K, Element)
N=No. of elements
K= Position

1. [Initialize counter] Set J:=N


2. Repeat steps 3 and 4 while (J>=K)
3. [Move Jth element downward] Set array[J+1]= array[J]
4. [Decrease counter] Set J:=J-1
[End of step 2 loop]
5. [Insert element] Set array[K]:= element
6. [Reset N] Set N:=N+1
7. Exit

Visual Representation
Let's take an array of 5 integers.
11, 12, 5, 90, 20.
If we need to insert an element 80 at position 2, the execution will be,
Algorithm for deleting an element from an array:
Algorithm for inserting elements into an array:
Delete (array, N, K, element)
N=No. of elements
K= Position

1. [Initialize counter] Set J:=K-1


2. Repeat steps 3 and 4 while (J>K)
3. [Move Jth element backward (left)] Set array[J]= array[J+1]
4. [Increase counter] Set J:=J+1
[End of step 2 loop]
5. [Reset N] Set N:=N-1
6. Exit

Visual Representation
Let's take an array of 5 elements.
11, 12, 5, 90, 20.
If we remove element 12 from the array, the execution will be,

The time complexity to insert and remove an element from the array is O(n) in the
worst case.

You might also like