Hey guys, what is up. In this particular post, we will be discussing what is an insertion, its algorithm, and source code. Also, we will be talking about how to delete an element from an array (deletion), its algorithm, and a source code. At last but not least there is a mind-blowing question, in the end, a must-read.
Updated on 29/9/2021
KEY POINTS
INSERTION:
The word insertion means to insert an element in an array. That array can be in a sorted or unsorted manner. This operation is helpful when we need to add elements in an array that are left before by mistake. First of all, we will all the elements in their next position to get the space at the location where we want to add a new element. This is possible with the help of a while loop which will work in reverse order. From n-1 to position minus one. Now the location is empty. Now insert the value at the location.
Insert 43 in location 2
ARRAY | PASS 1 | STAIR 2 | STEP 3 | PASS 4 |
A[0]=5 | 5 | 5 | 5 | 5 |
A[1]=88 | 88 | 88 | 43 | |
A[2]=66 | 66 | 88 | 88 | |
A[3]=54 | 66 | 66 | 66 | |
54 | 54 | 54 | 54 |
INSERTION ALGORITHM:
Step 1: Taking data from the user
Step 2: Position where data is to be inserted
Stair 3: i=n-1
Step 4: Repeat steps from 5 to 6 using while loop i>=pos-1
Step 5: A[i+1]=A[i]
Stair 6: i=i-1
{END OF LOOP}
Step 7: A[pos-1] = data
Step 8: n=n+1
Stair 9: Stop
INSERTION PROGRAM:
#include <iostream.h>
#include<conio.h>
int main()
{
clrscr();
const max=20;
int pos, A[max], n, i, data;
cout<<“Enter the dimension of array (<20) “;
cin>>n;
cout<<“Enter elements: “;
for (i=0; i<n; i++)
{
cout<<” A[“<<i<<“]= “;
cin>>A[i];
}
cout<<endl<<“Enter the element you want to insert= “;
cin>>data;
cout<<“Enter position where you want to add data= “;
cin>>pos;
i=n-1;
while (i >= pos-1)
{
A[i+1]=A[i];
i=i-1;
}
A[pos-1]= data;
n=n+1;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<“A[“<<i<<“]= “;
cout<<A[i]<<endl;
}
return 0;
}
DELETION:
In simple words, deletion means to delete something. In an array, it helps delete elements from an array. Moreover, we will first find the location of an array using a loop. If data is not found then we will print data not found. Else we will shift all elements to one location minus one using the loop.
Delete 43 from position 2
ARRAY | PASS 1 | STAIR 2 | STEP 3 | PASS 4 |
A[0]=5 | 5 | 5 | 5 | 5 |
A[1]=43 | 43 | 88 | 88 | 88 |
A[2]=88 | 88 | 66 | 66 | |
A[3]=66 | 66 | 66 | 54 | |
A[4]=54 | 54 | 54 | 54 |
DELETION ALGORITHM:
Step 1: i=0, pos= -1
Step 2: Take the data which is to be deleted
Stair 3: Repeat steps from 4 to 5 using while loop i<n
Step 4: if ( data= A[i])
pos=i
break
Step 5: i=i+1
{END OF LOOP}
Stair 6: if ( pos= -1)
Print data not found
Stair 7: i=pos
Step 8: Repeat steps from 9 to 10 using while loop i<n-1
Step 9: A[i] = A[i+1]
Stair 10: i=i+1
{END OF LOOP}
Step 11: n=n-1
Step 12: Stop
DELETION PROGRAM:
#include <iostream.h>
#include<conio.h>
int main()
{
clrscr();
const max=20;
int pos, A[max], n, i, data;
cout<<“Enter the dimension of array (<20) “;
cin>>n;
cout<<“Enter elements: “;
for (i=0; i<n; i++)
{
cout<<” A[“<<i<<“]= “;
cin>>A[i];
}
cout<<endl<<“Enter the element you want to delete= “;
cin>>data;
i=0;
pos= -1;
while (i <n)
{
if (A[i]==data)
{
pos=i;
break;
}
i=i+1;
}
if ( pos== -1)
cout<<”DATA NOT FOUND IN THE LIST”;
else
{
i=pos;
while(i < n-1)
{
A[i]=A[i+1];
i=i+1;
}
n=n-1;
cout<<endl;
for(i=0;i<n;i++)
{
cout<<“A[“<<i<<“]= “;
cout<<A[i]<<endl;
}
}
return 0;
}
Question: Write a program to insert data in an array A[n] where A is sorted in ascending order. Position it in the array in the sorted manner itself.
I will give the solution to this question in the next post.
In the upcoming post, you will be getting information related to the working of sensors and Merge two sorted arrays and unsorted arrays. If you haven’t read our recent post on insertion sort, its algorithm, and the program then go and read this. Also, stay tuned for the upcoming posts.