PDA

View Full Version : Sort this...



biterman
10-18-2001, 03:05 AM
You have 4 variables (A, B, C, D). And you have to sort them for largest to smallest. What is the best way to do this, that is the way which makes the least comparisons.

I don't know anything about algorithms (sorting), so all the info on that didn't really help me.

I think the best way would be this:

Step 1: Compare A to B
Step 2: Compare B to C
Step 3: Compare the two biggest
Step 4: Compare the two smallest.

Am i right?

adios,
biterman.

adrianxw
10-18-2001, 03:49 AM
One of numerous sort algorithm sites with cute animations...

http://www.geocities.com/SiliconValley/Network/1854/Sort1.html

... although if you only have 4 variables, it hardly seems worth it!

greenRoom
10-18-2001, 04:05 AM
that was a very cool applet.

biterman
10-18-2001, 04:33 AM
But the ignorant have to start learning somewhere... myself being the ignorant.

Can you point me to some literature on the subject that might lessen my ignorance? Or is all this just a question of knowledge in mathematics and how to implement it in programming languages?

By the way, what would be the most efficient (fastest) way to sort those (admittedly few) variables? In essence, was i right? If not, what is the right answer?

Thank you,
biterman.

adrianxw
10-18-2001, 05:20 AM
As to are you right, I don't know, you say compare, but what are you doing with them after you compare them? You should really try to think this out for yourself, it is a good exercise in problem solving/logical thinking.

If you can't, the link I gave you had a link to get the sort source, it is in java, but should be easy enough for a C programmer to follow. An alternative link with C code is...

http://www.programmersheaven.com/zone3/cat475/

biterman
10-18-2001, 05:38 AM
but think about it for myself is what i have done for several days, not to say a week or more. Of course, i arrived at the conclusion i shared with you after only a few minutes of pondering upon it. But being unsure of whether i was right or not, and not knowing how to prove one way or the other, i decided to keep pondering on it, and so i did. Then i thought it would be interesting to know what all of you might think, and maybe you would be able to tell me if i was right or not.

It is a good problem indeed.

thank you,
dario.