PROGRAMMING PROBLEMS & SOLUTIONS
Pages
HOME
URI
UVA
CODEFORCES
HACKERRANK
EXTRA
CONTACT
Sunday, February 12, 2017
Insertion Sort
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[100];
int n,x;
cin>>n;
int j;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=1;i<n;i++){
x=a[i];
for(j=i;j>0;j--){
if(a[j-1]>x)
a[j]=a[j-1];
else
break;
}
a[j]=x;
}
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment