How to display 3 minimum values in an array.

Hey guys, im just wondering how do we display 3 minimum values for in a single array.Do we loop 3 times or one is enough?
Hi,
> Do we loop 3 times or one is enough?
Three times are needed. It should be the easiest that way.
So can you show us your code?
im just wondering how do we display 3 minimum values for in a single array

That really depends on the size of the array. With a small array (less than 10 to 100 elements) iterating through the loop might be adequate but with a large array I would consider sorting, then just printing the first/last three elements.
@jib
Good idea.
heres the code:

printf("You have selected 1-room\n");
Min = rm1[0];





for (j = 0; j < 5; j++)
{

if (rm1[j] < Min)
Min = rm1[j];
k + 1;
}

printf("The min is %.2f on \n", Min);
puts(month[k]);



for (j = 0; j < 9; j++)
{
if (rm1[j] < 5)continue;
else (rm1[j] < Min);
{
Min = rm1[j];
k= k + 1;
}


}
printf("The min is %.2f on \n", Min);
puts(month[k]);

for (j = 0; j < 12; j++)
{
if (rm1[j] < 9)continue;
else (rm1[j] < Min);
{
Min = rm1[j];
k = j ;
}

}
printf("The min is %.2f on \n", Min);
puts(month[k]);



is this the right way to do it?
Last edited on
@jlb the array has 12 values in it xD
closed account (48T7M4Gy)
A single pass might be possible if this is any indication:

http://www.geeksforgeeks.org/to-find-smallest-and-second-smallest-element-in-an-array/
closed account (48T7M4Gy)
is this the right way to do it?

Did it work?

PS 'continue' is unusual.
@ kemort thanks for the link ill take a look at it and yeah it works
@Raizel
Can you post your entire code? Your code looks pretty fuzzy now.
For variety, boost has a library that can do this, and tons more statistics on a sequence of numbers, all in a single pass (although for this specific use case, it's more complicated than a trivial single-pass solution)

1
2
3
4
5
accumulator_set<int, features<tag::tail<left>>> acc(tag::tail<left>::cache_size = 3);
acc = std::for_each(std::begin(a), std::end(a), acc);
std::cout << "The 3 smallest values are ";
for(int n : tail(acc))
    std::cout << n << ' ';


full demo: http://coliru.stacked-crooked.com/a/3b203377617fe343

a couple other ways:

standard, boring, single-pass with a temporary container:

1
2
3
4
5
6
7
8
std::vector<int> m;
for(int n: a) {
    m.insert(std::lower_bound(m.begin(), m.end(), n, n);
    if(m.size() > 3) m.pop_back();
}
std::cout << "The 3 smallest values are:\n";
for(int x: m) std::cout << x << ' ';
std::cout << '\n';


in-place and not wasting time to keep the bottom 3 in sorted order:

1
2
3
4
std::nth_element(std::begin(a), std::begin(a)+3, std::end(a));
std::cout << "The 3 smallest values are:\n";
for(int n = 0; n < 3; ++n) std::cout << a[n] << ' ';
std::cout << '\n';
Last edited on
Here's an algorithm I just wrote up that does it in one pass:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

int main ()
{
    int arr[] = {9, 2, 4, 7, 7, 0, 7, 7, 12, 1, 5, 0, 12, 34};
    int SIZE = sizeof(arr)/sizeof(arr[0]);

    std::sort(arr, arr + 3);
    int min1 = arr[0];
    int min2 = arr[1];
    int min3 = arr[2];

    for(int i = 3; i < SIZE; i++)
    {
        if( arr[i] < min1 )
        {
            min3 = min2;
            min2 = min1;
            min1 = arr[i];
        }

        else if( arr[i] < min2 )
        {
            min3 = min2;
            min2 = arr[i];
        }

        else if( arr[i] < min3 )
        {
            min3 = arr[i];
        }
    }

    std::cout << "\nThree smallest: " << min1 << " " << min2 << " " << min3 << std::endl;

  return 0;
}


Note that this algorithm prints repetitions, so the output for above will be: 0, 0, 1 as 0 is repeated twice. The algorithm in geeksforgeeks does not count repetitions.
Topic archived. No new replies allowed.