Like Tree1Likes
  • 1 Post By Adak

Sorting variables with an optimized, specific process

This is a discussion on Sorting variables with an optimized, specific process within the C Programming forums, part of the General Programming Boards category; Hello everybody, so i'm writing a very simple program where the user inputs 3 ints *a, *b, *c, and there ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Sorting variables with an optimized, specific process

    Hello everybody,

    so i'm writing a very simple program where the user inputs 3 ints *a, *b, *c, and there is one function that handles the addresses of the integers and sorts them in ascending order. Only problem is, I can not use sort algorithms but specifically use an optimized process that is specific to three items.

    I'm having a hard time thinking out of the box on this one, being restricted on various sorts.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    I would be thinking making some "?:" statements

    [edit]
    A bubble sort might be best for this small application
    [/edit]
    Last edited by Click_here; 10-02-2012 at 07:24 PM.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    I thought that too but being simplistic and optimal is the whole challenge that this is supposed to be and i feel it would be quite a lot of code with those

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Think about it this way - You have three inputs: The most amount of changes you would need to do is three

    3 2 1
    v v v
    2 3 1
    2 1 3
    1 2 3

    The order of the check/swap never has to change because you know that you only have three inputs
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Forget any sorting code. There is an optimized set of if statements, made just for three items.

    To discover it, work it through by hand (don't try sit down and start coding), several times, with 3 coins of different values. You'll need to repeat it until you can see the pattern you are using, to sort them. Once won't be enough.

    When you see the pattern, keep doing it until it's really clear what the logic is that you're using. That's the backbone of your if statements.

    Only then are you ready to code anything.
    laserlight likes this.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Code:
    void sortThree(int *a, int *b, int *c);
    {
         int swap = 0;
         
         if (*c < *b)
         {
                swap = *c;
                *c = &b;
                *b = &c;
         }
         if (*b < *a)
         {
                swap = *b;
                *b = &a;
                *a = &b;
         }
         if (*b < *c)
         {
                swap = *b;
                *b = &c;
                *c = &b;
         }
    }
    I know this is wrong but am I kinda on the right track?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Kinda, yes.

    You are using if statements and representing the objects to be sorted with variables - that's good.

    But you didn't work it out by hand until you saw the pattern - so that stinks, and your logic is wrong because of it.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    I really like the advice to use some coins to work out a sorting algorithm!

    Let's go through your logic (for simplicity, I have omitted the dereferencing):
    a b c
    3 2 1

    v v v
    3 1 2 (swap cb if c<b)
    1 3 2 (swap ab if b<a)
    1 3 2 (swap bc if b<c)

    You are on the right track, but the logic is wrong

    Also think about this
    Code:
    *b = &c;
    /* The value in the variable 'b' is pointing to equals the address of the pointer 'c' */
    Last edited by Click_here; 10-02-2012 at 10:29 PM.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by slotown View Post
    Code:
    void sortThree(int *a, int *b, int *c);
    {
         int swap = 0;
         
         if (*c < *b)
         {
                swap = *c;
                *c = &b;
                *b = &c;
         }
         if (*b < *a)
         {
                swap = *b;
                *b = &a;
                *a = &b;
         }
         if (*b < *c)
         {
                swap = *b;
                *b = &c;
                *c = &b;
         }
    }
    I know this is wrong but am I kinda on the right track?
    That looks almost exactly right to me.

    Just try it out, there are only 5 possible ways that the data can be out of order. Write code to test out each possible ordering and if they all work, you're done!
    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. Replies: 21
    Last Post: 07-15-2012, 05:20 PM
  2. Which is more optimized?
    By Moony in forum C Programming
    Replies: 7
    Last Post: 08-21-2007, 01:36 AM
  3. Info on specific process
    By NoFearXD in forum Windows Programming
    Replies: 3
    Last Post: 05-20-2007, 07:42 PM
  4. Terminate Process by Specific FullFilePath
    By MattKamil in forum Windows Programming
    Replies: 1
    Last Post: 07-05-2006, 04:15 PM
  5. Sorting variables
    By Phoenix_Rising in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2005, 01:23 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21