Sign in to follow this  

Need help!

This topic is 4886 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! Thanks:
do{
  flag=0;
   for(b=1;b<final-1;b++)
   {
    if(votes[b]<=votes[b+1])
    {
	 stop = votes[b];
	 stops = pointers[b];
	 votes[b] = votes[b+1];
	 pointers[b] = pointers[b+1];
     votes[b+1] = stop;
	 pointers[b+1] = stops;
	 flag=1;
    }
   }
  final = final - 1;
 }while(flag==1);

[Edited by - drjulio2002 on September 1, 2004 6:44:27 PM]

Share this post


Link to post
Share on other sites
[google] Bubble Sort

Note the cons, bubble sort is more of a learning experience than a useful sorting algorithm.

Share this post


Link to post
Share on other sites

for(a=1;a<final;a++)
{
for(b=a;b<final-1;b++)
{
if(votes[b]<=votes[b+1])
{
stop = votes[a];
stops = pointers[a];
votes[a] = votes[b];
pointers[a] = pointers[b];
votes[b] = stop;
pointers[b] = stops;
}
}
}


You were comparing 'a' with every 'b', you were comparing the first element with every other one, then the next first element with all the others, and so on, wrong. You need to compare 'b', with 'b+1', so that it compares with one element, and the next one, then advances, and compares again, one element with the next one.
Also 'a' starts with '1', i'm not sure that's a good thing.

Share this post


Link to post
Share on other sites
Sign in to follow this