-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
  • 22,897
  • 3
  • 42
  • 92
Alex David
  • 585
  • 1
  • 11
  • 29

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
  • 464,258
  • 100
  • 912
  • 987
1

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

md5
  • 22,897
  • 3
  • 42
  • 92