So I am in a Java class in school, and I have a relatively simple assignment that I just can't figure out. It's not a problem to research the answer, so I am throwing it out to the brightest people out there. My simple java program takes 4 numbers as input, and then it is just supposed to spit those numbers right back out, but in order smallest to biggest. Anyone have any thoughts? I know there are probably lot of different ways to do this, but I am sure this can be done simply. My teacher for example, said that he can do it using only 6 non-nested or else-using if statements! I think he got into my head a little and so I am getting hung up. Any help out there would be greatly appreciated. Thanks
-
Please post your attempt so far. People generally don't like to just write your code for you.Mitch Wheat– Mitch Wheat2010-01-31 05:48:44 +00:00Commented Jan 31, 2010 at 5:48
-
Thanks for your help, to be honest I really haven't put much code down other than just taking the numbers as input. I was wanting a point in the right direction, and it looks like mquander gave me just what I needed. Thank you Stackoverflow!!Jeff– Jeff2010-01-31 05:55:28 +00:00Commented Jan 31, 2010 at 5:55
Add a comment
|
1 Answer
Read up on bubble sort -- an unrolled implementation of bubble sort would solve this problem with exactly 6 comparisons, and is probably what your teacher has in mind.
3 Comments
Mitch Wheat
pls don't use Bubble Sort. Ever.
mqp
I would suggest that an introductory homework problem in which you need to sort 4 numbers with 6 comparisons is an appropriate place to use bubble sort.
Jeff
Hey thanks mquander, I appreciate the tip, it looks like thats exactly what I was looking for.