0

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

2
  • Please post your attempt so far. People generally don't like to just write your code for you. Commented 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!! Commented Jan 31, 2010 at 5:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.