# my code stock.com # Inserting An Element in A Sorted Array by Hemton Anto

This program takes input for 1D array and then sorts the array.
After which it we can add an element into the sorted array and it still remains sorted, with an added extra element.

### Snippet options

Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

```#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
system("cls");
cout << "\t\t Inserting An Element in A Sorted Array:\n" << endl;
cout<<"Enter the Number of Elements in the Array : ";
int i,j,n,Min,Pos,Ele,temp;
cin>>n;
int m = n+1;
int* Arr1D = new int[m];
for (i=0; i<n; i++)
{
cout<<"Enter the "<<i+1<<"st Element : ";
cin>>Arr1D[i];
}
cout<<"\nArray before Sorting:\n";
for (i=0; i<n; i++)
{
cout<<Arr1D[i]<<"\t";
}
for (i=0; i<(n-1); i++)
{
Min = Arr1D[i];
Pos = i;
for (j=i+1; j<n; j++)
{
if (Arr1D[j] < Min)
{
Min = Arr1D[j];
Pos = j;
}
}
Arr1D[Pos] = Arr1D[i];
Arr1D[i] = Min;
}
cout<<"\n\nArray after Sorting:\n";
for (i=0; i<n; i++)
{
cout<<Arr1D[i]<<"\t";
}
cout<<"\n\nEnter the Element to be Inserted :\t";
cin>>Ele;
Arr1D[n] = Ele;
for(i=0; i<m; i++)
{
for(i=0; i<(m-1); i++)
{
for(j=0; j<(m-i-1); j++)
{
if( Arr1D[j] > Arr1D[j+1] )
{
temp = Arr1D[j];
Arr1D[j] = Arr1D[j+1];
Arr1D[j+1] = temp;
}
}
}
}
cout<<"\nFiNAL Array after Sorting:\n";
for (i=0; i<m; i++)
{
cout<<Arr1D[i]<<"\t";
}
delete [] Arr1D;
Arr1D = NULL;
cout<<endl;
return 0;
}```

### Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.