snippetcppModerate
Sort three numbers using only if-statements
Viewed 0 times
threestatementsnumbersusingsortonly
Problem
Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using
ifs. Any thoughts on how to improve this?#include
using namespace std;
int main ()
{
int num1, num2, num3;
int smallest, middle, biggest;
cin >> num1 >> num2 >> num3;
cout num3)
{
biggest = num2;
middle = num3;
}
}
if ((num1 num2) && (num3 > num1))
{
middle = num1;
if (num2 num3)
{
biggest = num2;
smallest = num3;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num3 > num2)
{
middle = num3;
smallest = num2;
}
}
if ((num1 > num2) && (num1 > num3))
{
biggest = num1;
if (num2 > num3)
{
middle = num2;
smallest = num3;
}
}
cout << smallest << ", " << middle << ", " << biggest << endl;
return 0;
}Solution
I will try to take several things into account but to keep things simple. A few remarks:
-
First, it seems that you have a typo here:
I think that you meant
-
First, it seems that you have a typo here:
if ((num1 < num2) && (num3 << num1))I think that you meant
num3
-
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in main) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:
void sort3(int& a, int& b, int& c);
You would give three variables to it and it would sort them in-place so that you end up with \$ a \le b \le c \$.
-
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).
void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}
Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.
Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
-
This is not a problem in your case, but we can't stress out enough that using namespace std; is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a .cpp file but it's better to keep that in mind.
-
You don't need to return 0; at the end of main. If the compiler reaches the end of the main function without having encountered a return statement, it automagically adds a return 0; for you. Note that it only works with main though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return 0`.Code Snippets
if ((num1 < num2) && (num3 << num1))void sort3(int& a, int& b, int& c);void sort3(int& a, int& b, int& c)
{
if (a > b)
{
std::swap(a, b);
}
if (b > c)
{
std::swap(b, c);
}
if (a > b)
{
std::swap(a, b);
}
}Context
StackExchange Code Review Q#91917, answer score: 16
Revisions (0)
No revisions yet.