views:

302

answers:

1

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

+1  A: 

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.

mquander
pls don't use Bubble Sort. Ever.
Mitch Wheat
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.
mquander
Hey thanks mquander, I appreciate the tip, it looks like thats exactly what I was looking for.
Jeff