Hi! I'm very new to Java, so please be gentle *_* I'm trying to write a .java file that will get three numeric values from the user, sort them in ascending order, and print the sorted values. This is the basis for the program:The most important thing is to be able to make this work with ONLY 3 IF statements. Can anyone help? I assume it's pretty easy...but I have not been able to figure it out. Thank you so much in advance... /JM [ edited to preserve formatting using the [code] and [/code] UBB tags -ds ] [ March 02, 2003: Message edited by: Dirk Schreckmann ]
why not use an array of integers instead. try this (selection sort):-
im not sure if it is correct. basically how it work :- 1) find the smallest element of the array(from index j+1 to the end of the array) 2) swap it with index at k hope this helps. [ March 02, 2003: Message edited by: Syamsul Hussin ]
Put 3 coins of different size on the tabletop. What comparisons do you need to make to see which is the biggest, smallest and the mid-sized coin? Once you have a system that works, see if you can reduce it to 3 comparisons. Sometimes it helps to (1) assume that whatever your looking for is the first one you get then (2) compare at all others and if you assumtion proves wrong, change your choice. [ March 02, 2003: Message edited by: Elouise Kivineva ]
You could also do it with no ifs if you use the Arrays class and the appropriate sort() method.
Yes, and actually, anything else would be a suboptimal solution. Why code the buble-heap-selection-quicksort routines from scratch if you have a standard, well tested, language-supported functionality? Incidentally, the default sort implementation in JDK1.4.1 is the modified mergesort which guarantees Nlog(N) performance. Eugene.