2

I'm currently stuck on a C++ problem that asks me to sort five inputted integers from least to greatest. I've tried using if statements to do this, but it just seems way too inefficient to list out all the possible combinations. I've also experimented with the min and max functions, but these only work on the smallest and largest two integers. What is the fastest way to sort them?

#include <iostream>
using namespace std;

int main() {

    cout << "Enter five integers. \n\n>";
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;

    int first = min({a, b, c, d, e});
    int fifth = max({a, b, c, d, e});

    cout << first << fifth << "\n"; //This is where I want to output the middle three integers in order

    cout << "\n";
    return 0;
}

Here's my code so far. ^

Anthony
  • 81
  • 7
  • 3
    Put in a standard container and pass to `std::sort`? – Some programmer dude Jul 29 '20 at 16:22
  • 3
    You should use an array and [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort). – MikeCAT Jul 29 '20 at 16:22
  • 2
    I'm always intrigued by this adjective `fastest`. Used like this is has nothing to do with speed. It means something like with the least amount of code, or without anything I don't currently understand. All perfectly fine things for a newbie to ask for, but I wonder why fastest, why not say simplest or easiest. – john Jul 29 '20 at 16:37
  • Ironically, laying out all the possible combinations probably is the fastest way to sort five numbers, when fastest really does mean takes less CPU time than any other method. – john Jul 29 '20 at 16:44

4 Answers4

3

The standard library has a function called std::sort, which works on any range. You can either put the numbers in an array, or you can put them in some other contain (like std::vector). In this case, arrays are simple enough given the five-item restriction. For example:

#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>

int main() {
    std::array<int, 5> numbers;

    std::cout << "Enter five integers. \n\n>";

    // Read numbers into array.
    for (auto & i : numbers) {
        std::cin >> i;
    }

    // Sort the entire array.
    std::sort(std::begin(numbers), std::end(numbers));

    // Display sorted numbers.
    for (auto i : numbers) {
        std::cout << i << '\n';
    }

    return 0;
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
3

The big mistake here is to read the values into individual variables. C++ has many different containers that can hold multiple values: an array, std::vector, std::array. But there's one that has a magic property, it keeps all the items in sorted order at all times: std::multiset.

cout << "Enter five integers. \n\n>";
std::multiset<int> a;
for (int i = 0; i < 5; ++i)
{
    int v;
    cin >> v;
    a.insert(v);
}
for (auto it = a.begin(); it != a.end(); ++it)
    cout << *it;
cout << "\n";
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

You can use a std::vector to store the input then use std::sort to sort the elements.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
    vector <int> nums(5);
    
    for(int i = 0; i < 5; ++i){
        cin >> nums[i];
    }

    sort(nums.begin(), nums.end());
}
ahskdjfk
  • 919
  • 5
  • 8
2

Alternative to std::sort, you could use std::multiset to sort your elements:

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int main(){
    multiset <int> nums;
    int input;
    
    for(int i = 0; i < 5; ++i){
        cin >> input;
        nums.insert(input);
    }
}