for(int i=0;i<length;i++)
should be
for(int i=0;i<length - 1;i++)
since you can have length - 1
number of comparisons.
for(int i=0;i<length;i++)
should be
for(int i=0;i<length - 1;i++)
since you can have length - 1
number of comparisons.