Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar06 / insertSorted.cpp
blob: 5bed69434105333ea537261a98f4c1f3f738c31d (plain) (blame)
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
#include<iostream>
#include<list>
#include<algorithm>

template<class T>
void insertSorted(std::list<T>& ll, const T& elem) {
    auto iter = ll.begin();

    while(iter != ll.end()) {
        if(*iter > elem) {
            ll.insert(iter, elem);
            break;
        }
        ++iter;
    }

    if(iter == ll.end())
        ll.push_back(elem);
}

int main() {
    std::list<int> ll;

    srand(time(NULL));

    for (size_t i = 0; i < 55555; i++)
        insertSorted(ll, rand());

    std::cout << std::is_sorted(ll.begin(), ll.end());
}