diff options
add Seminar06
1 files changed, 30 insertions, 0 deletions
| -rw-r--r-- | Seminar06/insertSorted.cpp | 30 |
diff --git a/Seminar06/insertSorted.cpp b/Seminar06/insertSorted.cpp new file mode 100644 index 0000000..5bed694 --- /dev/null +++ b/Seminar06/insertSorted.cpp @@ -0,0 +1,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()); +}
\ No newline at end of file |
