Results 1 to 6 of 6
  1. #1
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Sort 3 int's WITHOUT an aray.

    Hello everyone, I am new to the Java Forums (and Java itself) and so far, this site has been GREAT! I have been reading through some threads before, but now I need to ask a question so I thought it was time to create a profile!

    On an assignment I am doing, the professor has asked us to write a method that takes 3 integers as its input parameters (int a, int b, int c), sort the int's in order, and return a string with the numbers in order. AND I CAN NOT USE AN ARRAY! I was going good until i read that part lol.

    EXAMPLE: if (199, 8, 23) is passed, it should return "8 23 199".

    So how would I do this? I just need help with the physical sorting part.

    I started off with this just to get the first number, but it just doesnt feel right. There has to be a better way? And if this is the way, how would I get the second and third number in the correct order (least to highest)

    public static String inOrder(int a, int b, int c){
    String s = ("");
    if (a < b && a < c){
    s += (a + " ");
    }
    else if (b < a && b < c){
    s += (b + " ");
    }
    else{
    s += (c + " ");
    }
    return s;
    }


    Thanks in advanced for any help!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need a fourth variable to use as a swap (there are other ways, but this is the easiest). Then compare a and b and swap them if they are in the wrong order. Then compare b and c and swap them if they are in the wrong order. Then compare a and b again and swap them if they are in the wrong order (as c might have been the smallest).

  3. #3
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    You sir, are a genius! I was trying SOOO hard to do this one, but I couldnt figure it out without an array. (I hate how my professor is making us backtrack and not letting us use stuff that we learned >:-|

    Thanks again! this should work perfectly!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,438
    Blog Entries
    7
    Rep Power
    20

    Default

    Also have a look at this little trickery:

    Java Code:
    int a, b, c; // to be sorted
    int sa, sb, sc; // the sorted values
    
    // here goes:
    sa= Math.min(Math.min(a, b), c);
    sc= Math.max(Math.max(a, b), c);
    sb= a^b^c^sa^sc;
    This only works for ints of any size ...

    kind regards,

    Jos

  5. #5
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Ohh wow! thats a cool way of going about it! I should have been more specific though. When i said I can use an array, I meant to say I cant use ANYTHING other than .length(), .charAT, and .substring() from the string class :(

    He is trying to make use think outside the box a little, and boy is it working! :D

  6. #6
    ziaf is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    U can use recursion for this
    Java Code:
    private String test1(int a, int b, int c) {	 
    	 if(a <= b ) {
    		 if(b <= c) {
    			 return a+" "+b+" "+c;
    		 } else {
    			 return test1(a,c,b);
    		 }
    	 } else {
    		 return test1(b,a,c);
    	 }
    	 
     }

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. Replies: 15
    Last Post: 02-19-2009, 11:53 PM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM

Posting Permissions

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