- #1
waver.
- 8
- 0
hi Guys
i wrote a insertion sort code which is
it work fine with some size used in array and won't work with other size
i mean when i enter n=5 it work fine when i enter n=4 it won't work and sort is wrong
when n=5
and when n=4
the sort is wrong and error appear with last number
i just want to know what i did wrong . thanks in advance
i wrote a insertion sort code which is
Code:
#include <iostream> /*Waver*/
using namespace std;
int main()
{
int n,i,m;
float a[100],k;
cout<<"enter the amount of number";
cin>>n;
for(i=0; i<n; i++)
cin>>a[i];
for(i=1; i<=n; i++)
{
for(m=1 ; m<=i; m++)
{
if(a[m]<a[m-1])
{
k=a[m-1];
a[m-1]=a[m];
a[m]=k;
}
}
}
for(i=0; i<n; i++)
cout<<a[i] <<" ";
return 0;
}
i mean when i enter n=5 it work fine when i enter n=4 it won't work and sort is wrong
when n=5
the sort is wrong and error appear with last number
i just want to know what i did wrong . thanks in advance