hi Guys(adsbygoogle = window.adsbygoogle || []).push({});

i wrote a insertion sort code which is

it work fine with some size used in array and wont work with other sizeCode (C):#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 wont 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

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# C/++/# C++ sort by insertion proplem

**Physics Forums | Science Articles, Homework Help, Discussion**