Sorting

Is this possible to arrange given numbers in ascending order with if statement?

I have tried but, because of if else, it only prints the smallest of them but not any other number.

How to print all of them in ascending order with if statement?

My main question is that, is it possible to sort numbers with if statement?

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
#include <iostream>
using namespace std;
int main()
{
	float a, b, c;

	cout << "Enter first number: ";
	cin >> a;
	cout << "Enter second number: ";
	cin >> b;
	cout << "Enter third number: ";
	cin >> c;

	if (a < b && a < c)
		cout << a << endl;
	else
		{
		if (b < a && b < c)
			cout << b << endl;
		else
			{
			if (c < a && c < b)
				cout << c << endl;
			}
		}


	cout << endl << endl;
}
Last edited on
Sure, if you have n numbers, you need (roughly) n log n comparisons. Or, put another way, any finite sequence can be sorted in a finite number of comparisons.
Hint: Instead of printing them within the if statements, try modifying the logic to swap the numbers such that
a < b and b < c. Then, print a b c all at once.
Last edited on
Hello HelpMeBro,

Along with what Ganado said consider this:
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
#include <iostream>

using namespace std;

int main()
{
    double num1, num2, num3;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "Enter third number: ";
    cin >> num3;

    if (num1 < num2 && num1 < num3)
    {
        cout << num1 << '\n';
        // num1 is the lowest, so between num2 and num3 determine which is lowest and print it. Then print what is left. Same concept for the other 2.
    }
    else if (num2 < num1 && num2 < num3)
    {
        cout << num2 << '\n';
    }
    else  if (num3 < num1 && num3 < num2)
    {
        cout << num3 << '\n';
    }

    cout << "\n\n";
}


Andy
S.A.: sorting networks
https://en.wikipedia.org/wiki/Sorting_network

1
2
3
4
5
6
7
8
9
10
11
12
#include <utility>
#include <iostream>

int main()
{
  int a = 3, b = 2, c = 1 ;
  if (b > c) std::swap(b, c);
  if (a > b) std::swap(a, b);
  if (b > c) std::swap(b, c);

  std::cout << a << ' ' << b << ' ' << c << '\n'; 
}
See I'm always torn on things like this because I don't want to just give the answer right away.
But cool wiki link, didn't know that had a name.
Andy, where I have to do this pseudocode that you mentioned:

num1 is the lowest, so between num2 and num3 determine which is lowest and print it.
Then print what is left. Same concept for the other 2


In which statement, or line?
Last edited on
Ganado, I am here to learn programming. That's why, I have same motive. But there were 12 problems to practice. Those all problems will be solved by if statement. I have done 7 of them. This was the 8th one. But I am stuck here, and can't find any solution by my tiny brain. I don't have vast knowledge of if statement.
Last edited on
Hello HelpMeBro,

It was late for me and I realized later that there were a few things I did not mention.

When it comes to floating point prefer to use "double" over "float". I considered the possibility that what you are working from may be old when "float" was the normal choice.

The code demonstrates how a few blank lines make a big difference in readability.

Even though this is a small program a good variable name can still make a big difference.

When it comes to things like "if" statements it is a good practice to use {}s even for 1 line. In this example you will need to add to your if statements. Also there is nothing wrong with the way you first presented you if/else if statements and C++ does not have an "else if" as part of the language, but
1
2
3
else if
{
}

is what most people tend to use and what I have most often seen.

In a reply to Ganado you said:

But there were 12 problems to practice. Those all problems will be solved by if statement.


This is something that you should have mentioned at the very beginning so those responding will have a better idea what you have to do.

Your original code works very well at finding the lowest number and printing it out. But after you find the lowest number you still need to check the remaining 2 to decide which 1 to print first. So each of your original if/else if statements will have more choices to deal with until you have printed out all 3 numbers.

Then you may want to consider what would happen if any 2 of the 3 numbers is the same.

It would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (num1 < num2 && num1 < num3)
{
    cout << num1 << '\n';
    // num1 is the lowest, so between num2 and num3 determine which is lowest. Then print what is left. Same concept for the other 2.

    if (num2 < num3)
    {
        std::cout << num2 << '\n' << num3 << '\n';
    }
    else
    {
        std::cout << num3 << '\n' << num2 << '\n';
    }
}
else if (num2 < num1 && num2 < num3)


Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

double min(double a, double b)
{
	if (a < b) return a;
	return b;
}

int main()
{
	double num1 {}, num2 {}, num3 {};

	std::cout << "Enter first number: ";
	std::cin >> num1;

	std::cout << "Enter second number: ";
	std::cin >> num2;

	std::cout << "Enter third number: ";
	std::cin >> num3;

	std::cout << "lowest is: " << min(num1, min(num2, num3)) << '\n';
}

Andy, thanks for your vast answer. I really appreciate it. I learnt a lot of things from your single answer. And, it is solved now.

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
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
int main()
{
	float num1, num2, num3;

	cout << "Enter first number: ";
	cin >> num1;
	cout << "Enter second number: ";
	cin >> num2;
	cout << "Enter third number: ";
	cin >> num3;


    if (num1 < num2 && num2 < num3)
    {
        cout << num1 << endl << num2 << endl << num3;
    }

    else if (num3 < num2 && num2 < num1)
    {
        cout << num3 << endl << num2 << endl << num1;
    }

    else if (num2 < num3 && num3 < num1)
    {
        cout << num2 << endl << num3 << endl << num1;
    }

    else if (num2 < num1 && num1 < num3)
    {
        cout << num2 << endl << num1 << endl << num3;
    }

    else if (num3 < num1 && num1 < num2)
    {
        cout << num3 << endl << num1 << endl << num2;
    }

    else if (num1 < num3 && num3 < num2)
    {
        cout << num1 << endl << num3 << endl << num2;
    }
		


	cout << endl << endl;
}


Thanks!
Last edited on
Topic archived. No new replies allowed.