• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help with sorting numbers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you got so far? What don't you understand?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic