Website Summary Files Commits Branches @commit @diff @stats
path: root / Practicum / pract03 / partition.cpp
diff options
context:
space:
mode:
Commitdbd2a89e755ac4941f3afbf3bf78ecb810d1bf55 (patch)
Author
 Desislava Shunina <115353472+desiish@users.noreply.github.com>
2024-10-22 17:37:35 +0300
Committer
 GitHub <noreply@github.com>
2024-10-22 17:37:35 +0300
Files7d2368adb4e0de27d259d2e4ab6d60a897de37be /Practicum/pract03/partition.cpp
Data-structures-and-algorithms-dbd2a89e755ac4941f3afbf3bf78ecb810d1bf55.tar
Data-structures-and-algorithms-dbd2a89e755ac4941f3afbf3bf78ecb810d1bf55.tar.gz
Data-structures-and-algorithms-dbd2a89e755ac4941f3afbf3bf78ecb810d1bf55.zip
Parent20af6cae13ae020bc167008bb4e52a4cf01d911b (diff)
Add pract03 readme, merge and partition implementations
1 files changed, 42 insertions, 0 deletions
-rw-r--r--Practicum/pract03/partition.cpp42
diff --git a/Practicum/pract03/partition.cpp b/Practicum/pract03/partition.cpp
new file mode 100644
index 0000000..dd69876
--- /dev/null
+++ b/Practicum/pract03/partition.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <vector>
+
+bool isEven(int n) {
+ return !(n % 2);
+}
+
+template <class RandomIt, class Predicate>
+RandomIt partition(RandomIt first, RandomIt last, Predicate p)
+{
+ RandomIt beg = first;
+ for (; beg != last; ++beg)
+ {
+ if (!p(*beg))
+ break;
+ }
+ if (beg == last)
+ return beg;
+
+ for (RandomIt it = std::next(beg); it != last; ++it)
+ {
+ if (p(*it))
+ {
+ std::swap(*it, *beg);
+ ++beg;
+ }
+ }
+
+ return beg;
+}
+
+int main()
+{
+ std::vector<int> v;
+ for (int i = 1; i <= 10; i++)
+ v.push_back(i);
+
+ partition(v.begin(), v.end(), isEven);
+
+ for (int i = 0; i < 10; i++)
+ std::cout << v[i] << " ";
+}