programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

# Help with sorting numbers

Greenhorn
Posts: 2
Please can someone help me sort five numbers (input by the user) ranging from the lowest to the highest to calculate the arithmetic mean.
Thank You

Sheriff
Posts: 9099
12
What have you got so far? What don't you understand?

Bartender
Posts: 2205
You might want to take a look at java.util.Arrays.

Rob

Harold Bishop
Greenhorn
Posts: 2
Thanks Rob but we are not allowed to use arrays.
I just dont understand how to sort from the lowest value to the highest i.e...
num1 = 5 num2 = 55 num3 = 9 num4 = 2 num5 = 72, so it should be like this
2, 5, 9, 55, 72

Ranch Hand
Posts: 3143
Then take a look at the Collections API ... you might find something usful there ... some of the Classes that implement Collection automatically sort for you!

Sheriff
Posts: 7023
Hello,
If you're just trying to learn the concepts of different sorting routines, then this is a decent place to begin (it does use arrays, but their use is not required, especially if you have just five numbers to sort):
Chapter 4 of Handbook of Algorithms and Data Structures
The Bubble Sort is a good starting point. The basic concept of the Bubble Sort is to go through the list one item at a time comparing it to each item and switch their order if the second one is smaller (or bigger if that's how you're using it). An excellent advanced sorting method is the Merge Sort.
For more leads to understanding sorting concepts, go to the top-right of this page and click on search. I find this is often a good place to start with any of my questions, as many of these topics have been discussed at JavaRanch before.
The use of Collections will enable you to sort many (if not all) things without having to code your own sorting methods. But, it won't teach you much about how sorting works and it can be a very slow option - depending on what you are sorting and how you want to sort it.
Lastly, if you wanted to post your code, I (and I'm sure others) would be happy to review it and make suggestions.
Good Luck,
-Dirk Schreckmann