Skip to content

Commit ea99aa7

Browse files
authored
Create Insertionsort.cpp
1 parent 3c66256 commit ea99aa7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Insertionsort.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// code of the insertion sort
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
int main()
5+
{
6+
int n;
7+
cout<<"Enter the size of the array you want to sort"<<endl;;
8+
cin>>n;
9+
int arr[n];
10+
cout<<"Enter the elements of the array"<<endl;;
11+
for(int i=0;i<n;i++)
12+
{
13+
cin>>arr[i];
14+
}
15+
int j,temp=0;
16+
for(int i=1;i<n;i++)
17+
{
18+
temp=arr[i];
19+
j=i-1;
20+
while(j>=0&&arr[j]>temp)
21+
{
22+
arr[j+1]=arr[j];
23+
j--;
24+
}
25+
arr[j+1]=temp;
26+
}
27+
for(int i=0;i<n;i++)
28+
{
29+
cout<<arr[i]<<" ";
30+
}
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)