Skip to content

Commit

Permalink
Merge pull request narayancseian#45 from batlohiya12/main
Browse files Browse the repository at this point in the history
Create Insertion sort.cpp
  • Loading branch information
narayancseian authored Oct 5, 2022
2 parents dd1b12c + 8c06593 commit a69660d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Array/Insertion sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* C++ Program to implement Insertion Sort using Array */

#include<iostream>

using namespace std;

int main()
{
int i,j,n,temp,a[30];

cout<<"Enter size of Array :: ";
cin>>n;
cout<<"\nEnter elements to the array :: \n";

for(i=0;i<n;++i)
{
cout<<"\nEnter "<<i+1<<" element :: ";
cin>>a[i];
}

for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;

while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}

a[j+1]=temp;
}

cout<<"\nAfter Insertion Sort, Sorted list is :: \n\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";

return 0;
}

0 comments on commit a69660d

Please sign in to comment.