4 integer in ascending order

Hi guys,

So let me start off by saying that I saw the old archived post on this topic, but I found it not really helpful, and there was no way to add more to it. Using only if/else statements (no loops) I need to write a code that sorts any 4 integers in ascending order. I really don't want to do this with the brute force method, but a hint I have been given is to sort the first two numbers, and use some more if/else then put the third number in the right spot, and then some more if/else to put the fourth number in the right spot. Supposedly 6 if/else statements will do the job. Unfortunately this just confused me even more!
The following is the most coherent code that I have so far, and I think its starting to look like brute force! I don't think its right because I would need some more if/else relationships to fully describe the relationships besides the 6 main ones listed below. It's incomplete so don't try to run it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  int main()
{
    int a, b, c, d, first, second, third, fourth;
    cout << "Please input 4 integers: ";
    cin >> a >> b >> c >> d;
    
    if ((a < b) && (c < d))
    {
        if ((a < c) && (a < d))
        {
            first = a;
        }
    }
    else if ((a < b) && (c > d))
    {
        
    }
    else if ((a > b) && (c < d))
    {
        
    }
    else if ((a > b) && (c > d))
    {
        
    }
    else if ((a < c) && (b < d))
    {
        
    }
    else if ((a < c) && (b > d))
    {
        
    }
return 0;
}
Last edited on
I am assuming you are not allowed to use std::sort. Is there anything else you are 'forbidden' from using? There are some clever solutions, if not.

It looks like you're going to be unrolling a bubble sort algorithm. Try to recall the worst-case scenario for bubble sort with 4 values.
Yep, we can't use that either. Nor min :). The only functions I can use are ones that I created myself...
I'll wiki bubble sort :)
Update: is there any sort of bubble sort available for if/else?
The only ones I've seen are for arrays and I can't use that either!
Last edited on
I think using loops is the only way to bubble sort, which means you can't use it...

This is the easiest way I can think of doing it using 6 if statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void swap(int& num1, int& num2)
{
    int temp = num1;
    num1 = num2;
    num2 = num1;
}

int main()
{
    int a, b, c, d;

    cout << "Please input 4 integers: ";
    cin >> a >> b >> c >> d;

    if (b < a)
        swap(a, b);

    if (c < b)
        swap(b, c);

    if (b < a)
        swap(a, b);

    if (d < c)
        swap(c, d);

    if (c < b)
        swap(b, c);

    if (b < a)
        swap(a, b);

    cout << a << ' ' << b << ' ' << c << ' ' << d;

    return 0;
}
fg109 wrote:
I think using loops is the only way to bubble sort, which means you can't use it...
Lookup "loop unrolling". You can always unroll a loop manually when you know the number of inputs.
So basically it's the code I wrote, doing the steps of the loop manually?
Yes, you unroll the loop into a bunch of similar statements that do the same thing.
fg109,
why would you have multiple (b < a) statements?
Thanks!

And I can't use void either :(

Yup ultra c++ noob, I know!
Last edited on
rokujohaven wrote:
why would you have multiple (b < a) statements?

Think of what would happen if the code only had a single one of those, and the input was 4 3 2 1.

Also, if you can only have the main function, then just move the code from the swap function into the main function. I only had it as a separate function so that the code would be shorter.
Topic archived. No new replies allowed.