#include<iostream>
using namespace std;
int main()
{
int size, a[100],temp,j,i;
cout<<"enter array size"<<endl;
cin>>size;
cout<<"enter array"<<endl;
for(i=0;i<size;i++)
{
cin>>a[i];
cout<<endl;
}
for(i=1;i<size;i++) // the zeroth index of this array sorting will remain untouch.
{
temp=a[i];
j=i-1;
while((j>=0)&& (a[j]>temp)) // if a[j](va,ue on zeroth index) have higher value than 1st index
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"sorted array"<<endl;
for(i=0;i<size;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
No comments:
Post a Comment