summaryrefslogtreecommitdiff
path: root/straight-selection.cpp
blob: 86c8f942a0d33f30a5169d805d34a13a20f52997 (plain)
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
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <ostream>

template<class T> void
straight_selection(std::vector<T> &array)
{
    for (int i = 0; i < array.size(); ++i)
    {
        for (int j = i; j < array.size(); ++j)
        {
            if (array[j] < array[i])
            {
                std::swap(array[i], array[j]);
                break;
            }
        }
    }
}

template<class T> std::ostream &
operator<<(std::ostream &stream, std::vector<T> &v)
{
    if (v.size() == 0) stream << "{ }";
    
    stream << "array = { ";
    for (int i = 0; i < int(v.size()) - 1; ++i)
        stream << v[i] << ", ";

    stream << v.back() << " }";
    return stream;
}

int
main()
{
    std::vector<int> array = { 2, 7, 12, 30, 11, 4, 12, 5, 20 };
    std::cout << array << std::endl;
    straight_selection(array);
    std::cout << array << std::endl;

    return 0;
}