-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExperiment 8.cpp
More file actions
48 lines (46 loc) · 798 Bytes
/
Copy pathExperiment 8.cpp
File metadata and controls
48 lines (46 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
void insertionsort(int a[],int n)
{
int i,j,k,temp,m;
for(i=0;i<n;i++)
{
m=a[i];
j=i;
while(a[j-1]>m && j>0)
{
a[j]=a[j-1];
j--;
}
a[j]=m;
}
}
int main()
{
int i,a[20],n;
cout<<"Enter no of elements in an array: ";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
insertionsort(a,n);
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
int item;
cout<<"\nEnter ITEM to be inserted : ";
cin>>item;
i = n-1;
while(item<a[i] && i>=0)
{
a[i+1] = a[i];
i--;
}
a[i+1] = item;
n++;
cout<<"\nAfter insertion array is :\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}