-1
do
{
    swap=false;
    for(int i=0; i<256; i++)
    {
    if(pd[i]<pd[i+1])
    {
        int temp=pd[i];
            pd[i]=pd[i+1];
            pd[i+1]=temp;
        swap=true;
        }
    }
}
while(swap);

It only returns top two results properly and the rest as 0. I am sorting floats.

md5
  • 23,373
  • 3
  • 44
  • 93
Alex David
  • 585
  • 1
  • 11
  • 32
  • 1
    You mean, you are comparing floating points using `<` operator? .. you need to use epsilon. Check this - http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values – Sangeeth Saravanaraj Apr 26 '12 at 14:23
  • 5
    @SangeethSaravanaraj: you only need epsilon values when doing equality checks. This is relational so it won't be a problem. – Skizz Apr 26 '12 at 14:24

2 Answers2

9

I am sorting floats.

In this case, temp must be of type float:

float temp=pd[i];

Otherwise, you're truncating pd[i] to int each time you do a swap.

Also, if your float array might contain NaNs, they'll require extra care in comparisons.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

temp is of type int, but your array is of type float...

md5
  • 23,373
  • 3
  • 44
  • 93