Thread: Sorting 3 ints

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Sorting 3 ints

    Hallo,

    I have a homework question where I have to sort 3 ints. I have done it, but I think there is a better way to do it?

    Anyone want to take a look?

    Code:
       int a,b,c;
        
        // Check if 'a' is the smallest one
        if( (a < b) && (a < c) )
        {
            cout << "A is smalles" << endl;
            
            // If 'a' is the smallest, check which is the biggest
            if(c < b)
                cout << "B is biggest" << endl;
            else
                cout << "C is biggest" << endl;
        }
        
        // Check if 'b' is the smallest one
        if( (b < a) && (b < c) )
        {
            cout << "B is smalles" << endl;
            
            // If 'a' is the smallest, check which is the biggest
            if(a < c)
                cout << "C is biggest" << endl;
            else
                cout << "A is biggest" << endl;
        }
    
        // Check if 'c' is the smallest one
        if( (c < a) && (c < b) )
        {
            cout << "C is smalles" << endl;
            
            // If 'a' is the smallest, check which is the biggest
            if(a < b)
                cout << "B is biggest" << endl;
            else
                cout << "A is biggest" << endl;
        }
    I cant use any of the sort functions.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    For comparing only 3 values, you aren't going to find a better way than just checking the possibilities. But you can imagine the pain of doing this for, say, 10 values instead of 3. And it would be completely impossible if it had to work for any number of values.

    So, I'd say your solution is fine.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Maybe use else if's instead of if when testing for the smallest. It'll stop unnecessary checks.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If by sorting it is meant that in the end a would contain the smallest value, b the middle one and c the largest (you are supposed to swap something), then 3 values could sorted with exactly 3 comparisions.

    Just do the same steps that a bubble-sort would do:
    Code:
    void bubble_sort(int *array, int count)
    {
        for (int i = 0; i != count - 1; ++i) {
            for (int j = 1; j != count - i; ++j) {
                if (array[j-1] > array[j])
                    std::swap(array[j-1], array[j]);
            }
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Pick any comparison as your first one, say a < b
    If that is true, what are the possibilities? cab, or acb, or abc.
    So, now check for the first possibility by testing if c < a. If so, you're done.
    If not, then try the second possibility by testing if c < b. If so it's a acb, otherwise its abc.
    Now assume a is not less than b (the else case)
    What are the possibilities? cba, or bca, or bac.
    So, now check for the first possibility by testing if c < b. If so, you're done.
    If not, then try the second possibility by testing if c < a. If so it's a bca, otherwise its bac.

    That's actually the whole thing there in pseudocode! No more than 3 tests are done to determine the resulting ordering, and sometimes even 2 is enough.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 06:12 AM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM