Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar06 / insertSorted.cpp
diff options
context:
space:
mode:
Commit0ada48f859ab59acdbd67e67e887026cacc29e33 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-09 22:56:24 +0200
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-09 22:56:24 +0200
Files4b4edeee0fc768ddf48073804c4a0f8719e580ed /Seminar06/insertSorted.cpp
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.tar
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.tar.gz
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.zip
Parent16a916c1fc7ecf89bfbf910445740fb4596aae8f (diff)
add Seminar06
1 files changed, 30 insertions, 0 deletions
-rw-r--r--Seminar06/insertSorted.cpp30
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