# decision making in sorting

• 10-20-2012, 11:05 PM
FOX427
decision making in sorting
Hi All! I spent more than three hours trying to solve this problem, basically I need to write a program that reads in three floating point numbers and prints the three inputs in sorted order. For example:
Enter 3 numbers:
4
9
2.5

Sorted order:
2.5
4
9

I did some coding(if you can say it coding :D), here is it:

[code]
import java.util.Scanner;

public class SortingOrder {

/**
* @param args
*/
public SortingOrder()
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter three numbers folowed by enter after each number");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
System.out.println(a+b+c);
}

public double getOrder ()
{
if ((a<b)&&(a<c))
{
first=a;
if (b<c) second=b;
else second=c;
if (c>b) third=c;
else third=b;
}
if ((b<a)&&(b<c))
{
first = b;
if (a<c) second=a;
else second=c;
if (c>a) third=c;
else third=a;
}
if ((c<a)&&(c<b))
{
first=c;
if(a<b) second=a;
else second=b;
if (b>a) third=b;
else third=a;
}
System.out.println("The new order is: "+"\n"+first+"\n"+second+"\n"+third+"\nSecond line executed");

return 0;
}

double first;
double second;
double third;
double a,b,c;

public static void main(String[] args) {
// TODO Auto-generated method stub
SortingOrder order=new SortingOrder();
System.out.println(order.getOrder());
}

}

[\code]

My question is can you help me to find another way of doing this sorting, it is obvious that my code is not best solution(in fact if it's not the worst), I am looking at it and asking myself what if I need more than 3 inputs?
• 10-21-2012, 02:54 AM
doWhile
Re: decision making in sorting
You have a few options. You can write your own (search the internet for sorting algorithms to find any number of ways to sort something), or place the values in a Collection or an Array, and use the sorting methods supplied by J2SE (Arrays.sort() or Collections.sort())
• 10-21-2012, 03:28 AM
killutch
Re: decision making in sorting
Quote:

Originally Posted by doWhile
You have a few options. You can write your own (search the internet for sorting algorithms to find any number of ways to sort something), or place the values in a Collection or an Array, and use the sorting methods supplied by J2SE (Arrays.sort() or Collections.sort())

:(y):

Code:

```public static void main(String[] args){                                 float[] test = {4f, 9f, 2.5f};                                 Arrays.sort(test);                                 for(float f: test)                         System.out.println(f);                                         }```
Resault =

2.5
4.0
9.0
• 10-21-2012, 06:40 AM
DarrylBurke
Re: decision making in sorting