Results 1 to 4 of 4
Like Tree3Likes
  • 1 Post By doWhile
  • 1 Post By killutch
  • 1 Post By DarrylBurke

Thread: decision making in sorting

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Smile 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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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())
    FOX427 likes this.

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: decision making in sorting

    Quote Originally Posted by doWhile View Post
    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())


    Java 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
    FOX427 likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: decision making in sorting

    FOX427 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. GregorianCalendar issue, can't use Decision making, help
    By ImWithStupid in forum New To Java
    Replies: 11
    Last Post: 04-14-2011, 03:17 AM
  2. Decision structure truth table
    By JMaste in forum New To Java
    Replies: 2
    Last Post: 07-14-2010, 08:31 AM
  3. c4.5 decision tree
    By emailtosridevi in forum SWT / JFace
    Replies: 1
    Last Post: 03-15-2010, 06:34 AM
  4. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 01:47 AM
  5. Java Looping and decision
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •