Header Ads

Learn Together, Stay Connected.

Insertion Sort -C++


 #include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen("in.txt","r",stdin);
int ara[] = {5,4,3,2,1 };
int n=sizeof(ara)/sizeof(ara[0]);

int i,j,k;

for(i=1;i<n;i++)
{
j=i-1;
int key=ara[i];

while(j>=0 && ara[j]>key)
{
ara[j+1]=ara[j];
j--;
}
ara[j+1]=key;


}
for(i=0;i<n;i++)
{
cout<<ara[i]<<" ";
}
cout<<endl;

return 0;
}

No comments

Theme images by Dizzo. Powered by Blogger.