From 0ada48f859ab59acdbd67e67e887026cacc29e33 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Wed, 9 Nov 2022 22:56:24 +0200 Subject: add Seminar06 --- Seminar06/insertSorted.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Seminar06/insertSorted.cpp (limited to 'Seminar06/insertSorted.cpp') 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 +#include +#include + +template +void insertSorted(std::list& 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 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 -- cgit v1.3.1-23-gd82d