#Displaying the smallest three elements in an array in the order they appear.

39 messages · Page 1 of 1 (latest)

desert fulcrum
#

I have an array of 10 integers. I want to display them as the title says. I've tried to keep track of the indexes, but that seems way too inefficient and ||rookie like||. I was wondering if you guys had any tips on how to proceed, thanks in advance.
I'm doing the code to input and output both exactly as specified, so expect shitty code.

#include <iostream>
#include <limits>
int main()
{
    int arr[10] = {0};
    for(int i = 0; i < 10; ++i)
    {
        std::cin >> arr[i];
    }
    int max = std::numeric_limits<int>::max();
    int min1 = max, min2 = max, min3 = max;
    int index[3] = {0};
    for(int i = 0; i < 10; ++i)
    {
        int atual = arr[i];
        if(atual < min1)
        {
            index[0] = i;
            min3 = min2;
            min2 = min1;
            min1 = atual;
        }
        else if(atual < min2)
        {
            index[1] = i;
            min3 = min2;
            min2 = atual;
        }
        else if(atual < min3)
        {
            index[2] = i;
            min3 = atual;
        }
    } // 12 23 13 - possible swaps
    if(index[0] > index[1])
    {
        std::swap(min1, min2);        
    }
    if(index[0] > index[2])
    {
        std::swap(min1, min3);
    }
    else
    {
        std::swap(min1, min2);
    }
    std::cout << min1 << std::endl;
    std::cout << min2 << std::endl;
    std::cout << min3 << std::endl;
    return 0;
}```
subtle coralBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

desert fulcrum
#

Thanks in advance.

#

Oh, and the swaps are all wrong, as you could probably tell.

near carbon
#

why not just use a partial sort?

desert fulcrum
#

I'll check that, thanks.

near carbon
#

basically does exactly what you want

desert fulcrum
#

It's described as a random access iterator defining the one-past-the-end iterator of the range to be sorted, so should I use std::end(arr) + 1, That doesn't look right, probably isn't.

near carbon
#

so middle is till where you want it to be sorted, so std::begin(), std::begin() + 3, std::end()

#

should do it in your case

desert fulcrum
#

Thanks!

near carbon
#
   std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
 std::partial_sort(s.begin(), s.begin() + 3, s.end());
#

for a std::array

#

syntax is a bit different for this array but basically just what you wrote

desert fulcrum
#

But what if the three smallest ones aren't on the first three elements? Say {4,5,6,3,1,2,8,7,7,7}.

#

Would it still work?

#

Nevermind, I'll test

near carbon
#

so from start to middle it's sorted the rest isn't

desert fulcrum
#

Oh, I see.

near carbon
#
std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
std::partial_sort(s.begin(), s.begin() + 3, s.end());
0 1 2 7 8 6 5 9 4 3 
      ^

and everything past 7 here is in some random order

desert fulcrum
#

Hmm, but that isn't exactly what I'm supposed to do. I must print them in the order they appear, so for {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}, it'd print 2, 1, 3.

#

Actually 2,1,0

near carbon
#

oh right 🤔 I didn't read that part

desert fulcrum
#

Oh, got an idea. I could traverse the array to find the smallest element. Then, traverse again but ignoring the smallest, to find the second smallest, then ignoring the first two to find the third, then printing it. Not three different traversals of course, I'll do it in one for.

#

Hope that works

#

Works in my mind at least

#

Nah it needs to be separate traversals, because I won't know the smallest until I've reached the end

#

Sounds inefficient.

near carbon
#

you could propably do it in a single pass, check the sum of 3 numbers, and keep the smallest sum / order

#

and those are your 3 digets

#

something like that might work as well

desert fulcrum
#

You're right, might as well try that.

near carbon
#

you can't do it quite by count alone but you can use it a bit

#

but something like that

subtle coralBOT
#

@desert fulcrum Has your question been resolved? If so, run !solved :)

desert fulcrum
#

!solved