# Sorting variables with an optimized, specific process

• 10-02-2012
slotown
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.
• 10-02-2012
Click_here
I would be thinking making some "?:" statements

A bubble sort might be best for this small application
[/edit]
• 10-02-2012
slotown
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
• 10-02-2012
Click_here
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
• 10-02-2012
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.
• 10-02-2012
slotown
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?
• 10-02-2012
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.
• 10-02-2012
Click_here
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

Code:

```*b = &c; /* The value in the variable 'b' is pointing to equals the address of the pointer 'c' */```
• 10-02-2012
iMalc
Quote:

Originally Posted by slotown
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!