The moose likes Beginning Java and the fly likes Help with sorting numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with sorting numbers" Watch "Help with sorting numbers" New topic

Help with sorting numbers

Harold Bishop

Joined: Jan 29, 2002
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
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
What have you got so far? What don't you understand?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
You might want to take a look at java.util.Arrays.


SCJP 1.4
Harold Bishop

Joined: Jan 29, 2002
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
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
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!

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Help with sorting numbers
It's not a secret anymore!