• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with sorting numbers

 
Harold Bishop
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you got so far? What don't you understand?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at java.util.Arrays.

Rob
 
Harold Bishop
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic