Anthony
Anthony

Reputation: 81

How do I order a set of integers from smallest to greatest without if statements?

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. ^

Upvotes: 2

Views: 1633

Answers (4)

Christopher Miller
Christopher Miller

Reputation: 3461

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);
    }
}

Upvotes: 0

Christopher Miller
Christopher Miller

Reputation: 3461

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());
}

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308158

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";

Upvotes: 3

cdhowie
cdhowie

Reputation: 169008

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;
}

Upvotes: 3

Related Questions