1
\$\begingroup\$

I was asked to write a program that sorts three strings alphabetically using only if-else (no sorting algorithms, usage of arrays whatsover). I'm fairly new to C++ programming but I've come up with this program and would like to know if there's any way to make this more efficient while only using if-else? Any help or suggestions would be appreciated. Thank you!

#include <iostream> using namespace std; int main() { string word1, word2, word3; string temp; cout << "Enter three words separated by a space: "; cin >> word1 >> word2 >> word3; if (word1 > word2 && word1 > word3 && word3 > word2) { temp = word1; word1 = word2; word2 = word3; word3 = temp; } else if (word1 > word2 && word1 > word3 && word2 > word3) { temp = word1; word1 = word3; word3 = temp; } else if (word2 > word1 && word2 > word3 && word1 > word3) { temp = word2; word1 = word3; word2 = word1; word3 = temp; } else if (word2 > word1 && word2 > word3 && word3 > word1) { temp = word2; word2 = word3; word3 = temp; } else if (word3 > word1 && word3 > word2 && word1 > word2) { temp = word1; word1 = word2; word2 = temp; } cout << "The correct sort is " << word1 << ", " << word2 << ", " << word3 << endl; return 0; } 
\$\endgroup\$
2
  • \$\begingroup\$This code does run, I've already tested it. And this code works perfectly fine, as mentioned it's supposed to sort the strings in alphabetical order. What my lecturer showed as an example was using if-else to sort 2 strings and we were then tasked to use the same concept to sort 3 strings. But I want to know if there's any way to improve this code to make it less bulky.\$\endgroup\$
    – Iy 716
    CommentedNov 4, 2020 at 9:04
  • \$\begingroup\$@Wale c++ string class has an overload for comparison operators that internally call the compare method of that class which performs a lexicographical compare on the strings.\$\endgroup\$
    – Lev M.
    CommentedNov 4, 2020 at 11:03

2 Answers 2

1
\$\begingroup\$

You could create a function that automatically places two of the strings.

#include <iostream> using namespace std; //returns true if correction was needed bool correctArrange(string& a, string& b){ if (a <= b) return false; auto temp = a; a = b; b = temp; return true; } int main() { string word1, word2, word3; string temp; cout << "Enter three words separated by a space: "; cin >> word1 >> word2 >> word3; for (bool changed = true;changed;){ changed = correctArrange(word1, word2) | correctArrange(word2, word3); } cout << "The correct sort is " << word1 << ", " << word2 << ", " << word3 << endl; } 

Basically implementing bubble-sort algorithm.

If you're not allowed even the lowly for-loop, then you can unroll it.

int main() { string word1, word2, word3; string temp; cout << "Enter three words separated by a space: "; cin >> word1 >> word2 >> word3; correctArrange(word1, word2); if (correctArrange(word2, word3)) correctArrange(word1, word2); cout << "The correct sort is " << word1 << ", " << word2 << ", " << word3 << endl; } 
\$\endgroup\$
6
  • \$\begingroup\$I'm not even sure that OP is allowed to use functions, but sure I can add that.\$\endgroup\$CommentedNov 4, 2020 at 11:30
  • \$\begingroup\$Welcome to CodeReview@SE. Your correct arrangement primitive looks much like the one used in sorting networks. (correctly arrange?)\$\endgroup\$
    – greybeard
    CommentedNov 4, 2020 at 11:34
  • \$\begingroup\$Hello, thank you for your response! However I'm a newbie in this so there's some parts I don't really understand, for instance can I know why the bitwise operator | is used here? And why is changed set to false in the if statement? Thanks again.\$\endgroup\$
    – Iy 716
    CommentedNov 4, 2020 at 12:44
  • \$\begingroup\$In the function: think of the return-value as "if correct", if the function is already correct, you don't need to do anything else. This additional information can then be used elsewhere! Since the return is a boolean you can use either the bitwise or word-wise or - either will work here. 0x01 | 0x01 == 0x01 || 0x00\$\endgroup\$CommentedNov 4, 2020 at 12:54
  • 1
    \$\begingroup\$@AlexShirley main() is special in C++ and C99+: return 0; is implicit.\$\endgroup\$CommentedNov 4, 2020 at 13:02
2
\$\begingroup\$
  1. Your indentation is off. if should be at the same level as the corresponding else or else if, their bodies being indented once. Don't wander all the way to the right.

  2. You are missing the include for std::string, <string>.

  3. using namespace std; seems convenient, right? Unfortunately, throwing everything and the kitchen sink into the global namespace and praying it works is error-prone.
    See "Why is “using namespace std;” considered bad practice?".

    I wouldn't even use a using-declaration here, though that would be unproblematic at least.

  4. return 0; is implicit for main(). (And only for that function.)

  5. You never test whether reading (and/or writing) succeeds. At least that's pretty harmless in your program. Still, consider ending with:

     return !!std::cin || !!std::cout; 
  6. Swapping is often far more efficient than copying. Consider using a.swap(b) or the more generic using std::swap; swap(a, b); two-step, the latter is in <utility>.

  7. Only use std::endl when you need to flush the stream. Actually, belay that, be explicit and use std::flush.
    Hint: std::cin and std::cout are tied, which you already take advantage of with your prompt. Also, when the program ends the standard streams are flushed.
    See "What is the C++ iostream endl fiasco?".

  8. Comparing strings is likely to be comparatively expensive. Thus, compare two strings, and then make different decisions depending on the outcome.

  9. An alternative to considering each case separately is writing a sorting-network.
    You would be well-advised to abstract a conditional swap into its own function if you go that way:

    void sort2(std::string& a, std::string& b) noexcept { if (b < a) b.swap(a); } 
\$\endgroup\$
3
  • \$\begingroup\$Hi! Thanks for your response. 2. Can I know when should I use #include <string>? Because my program ran normally without this header... 4. Does this mean I don't need to write return 0 for my main function? 5. Can I know how the testing reading (and/or writing) works? 6. Unfortunately I'm not allowed to use anything other than if-else, oof. But thanks for letting me know about the swap!\$\endgroup\$
    – Iy 716
    CommentedNov 4, 2020 at 14:30
  • \$\begingroup\$re2: Whenever you use something from a header, include it. Don't depend on your implementation today recursively including header X when you include Y unless contractual, that's brittle. re4: Yes. re5: See a reference on streams. re6: You are using plenty more. Anyway, how about keeping strictly to those handicaps for one version, and doing it right for another. Whether to submit both depends on the teacher.\$\endgroup\$CommentedNov 4, 2020 at 15:15
  • \$\begingroup\$thanks for your explanations, I really appreciate it :)\$\endgroup\$
    – Iy 716
    CommentedNov 5, 2020 at 6:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.