Advertisement

Is bubble sorting important

Started by March 14, 2002 10:05 PM
19 comments, last by code_fx 22 years, 11 months ago
I understand it to a degree well... Let me show you better yet limit = NUM_QUIZZES - 2; // I know what this means and the logic // but but just by looking it looks // like it has no relevance.. for (pass = 1; pass<=NUM_QUIZZES - 1; ++pass) // along with this { for (quiz = 0; quiz <= limit; ++quiz) //from here down is ez if (grade[quiz] > grade[quiz + 1]) { temp = grade[quiz]; grade[quiz] = grade[quiz + 1]; grade[quiz + 1] = temp; } --limit; } the pass part counts how many times it has moved up the array.. and limit well is a limit... so it will not go beyond the array limits but can someone explain the loop process or help to explain it better than the book for example why is it limit = NUM_QUIZZES - 2 and not another number..,. and why NUM_QUIZZES is minus 1 in the loop and so on... for those who answer thank you...
If you have moved n-1 items to their appropriate position, the last one is necessarily also at its appropriate position.

The additional -1 is due to the fact that C arrays are 0-based and that the comparison used <=. There are n items at positions [0 .. n-1]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
dont ever use bubble sort in a real world situation

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
quote:
dont ever use bubble sort in a real world situation


What if you are "sorting" two numbers?

Trying is the first step towards failure.
Trying is the first step towards failure.
So you guys are saying that bubble sorting really has no purpose.... what I am saying is should I stress my self with learning the procedures for doing it... I know there is a point when it is going to matter which number comes first...
You probably should spend some time to learn it, because most the other sorting algorithms are more complicated than the bubble sort (I think it''s the easiest). I think if you learn the bubble sort it''s easier to learn the other ones afterward.
Advertisement
quote:
Original post by ragonastick
What if you are "sorting" two numbers?



Then you would use a single if-else statement, not a nested for loop.

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that really matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
You don''t learn the bubble sort for it''s practical uses, you learn it because it''s a good start to learning algorithms in general. Learn what makes the bubble sort slow, learn what you can do to make it faster, think of other ways of doing it. Sorting algorithms are all pretty simple, but it''s a great place to learn how to forumlate your own algorithms - a very important skill.

Everything is worth learning!


codeka.com - Just click it.
And also, if you know that you''re only going to be sorting very few items (like 10-20), and speed isn''t a major issue (ie you''re not going to be sorting your 10 items 60 times a second), then bubble sort is easily fast enough, and very quick to code (which can occasionally be useful - like if you want to sort some thing for test purposes.

I''m not trying to say that bubble sort is good, just that it isn''t "the worst thing since goto" (goto can be quite useful as well ).

(this is probably a pointless post, I think I need to take some more sedatives - oh well)

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
quote:
Original post by Dean Harding
Everything is worth learning!

Just to be downright silly, I wanted to present an exception to this. How about the fact that giraffes are unable to cough ? Is that worth learning? Well, is it? Eh, eh?

This topic is closed to new replies.

Advertisement