Sign in to follow this  

How to bubble sort floats?

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Everyone! I can't bubble sort floating point numbers, I think I need to use pointers:
[Source]
void bubblesort(char skaters[][15], float averages[], int count)
{

 float stopt={0};
 char *stop={0},*names[10]={0};
 int a={0},b={0};
 
 for(count=0;count<10;count++)
 {
  names[count] = &skaters[count][0];
 }

 for(a=0;a<10-1;a++)
 {
  for(b=a+1;b<10;b++)
  {
   if(averages[a]<averages[b]);
   {
    stop=names[a];
    stopt=averages[a];
    names[a]=names[b];
    averages[a]=averages[b];
    names[b]=stop;
    averages[b]=stopt;
   }
  }
 }

}
[/Source]

Share this post


Link to post
Share on other sites
Syntax Error:
if(averages[a]<averages[b]); // <<--- the ';' should not be here!
{
stop=names[a];
stopt=averages[a];
names[a]=names[b];
averages[a]=averages[b];
names[b]=stop;
averages[b]=stopt;
}




Your extra ";" effectively makes your if() a pointless construct (my compiler warns me of this, maybe you should double check you've enabled warnings and not ignored this one!). Because of this, the swapping done between { and } will be executed for every iteration - generally not a lot of use to you [smile].


Quote:
I can't bubble sort floating point numbers

Something you may not be aware of is that comparing two floating point numbers is a bit of a grey area.

Normal bubble sort relies on lines like if(averages[a]<averages[b]) which, for small floats (or very similar floats) may not give the results you expect. For better results you want to compare them to an epsilon value.

I think there are some definitions of what epsilon should be, but its been a while since I've messed about with it (the number varies for comparing large floats or comparing very small floats iirc).

For example, equality should be something like if( fabs(averages[a] - averages[b]) < 0.000001f ). I think I saw a C-Macro that worked for LEQUAL/GEQUAL based on the signed bit in the representation, but as I said - it's been a while [smile]

hth
Jack

Share this post


Link to post
Share on other sites
Don't feel bad about using the STL:



#include <iostream>
#include <algorithm>

int main() {
float a[7] = {23.4F, 1.5F, 79.31F, -1.4F, 1.2F, 1.73F, 1.7F};

std::sort(a, a+7); // sort 7 elements

for (int i=0; i<7; i++) {
std::cout << a[i] << "\n";
}
std::cin.get();
return 0;
}

Share this post


Link to post
Share on other sites

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this