From d61b4004d2ab1ace0bbf2383704f1bd6d7526499 Mon Sep 17 00:00:00 2001 From: Hugo Sobral Date: Thu, 3 Oct 2019 14:36:12 -0300 Subject: [PATCH 1/3] Add partition description and 'Run Code' in 'algorithm' folder --- algorithm/Partition.md | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 algorithm/Partition.md diff --git a/algorithm/Partition.md b/algorithm/Partition.md new file mode 100644 index 00000000..d06b100d --- /dev/null +++ b/algorithm/Partition.md @@ -0,0 +1,48 @@ +# Partition + +**Description :** This function is used to partition the elements on basis of condition mentioned in its arguments. + +```cpp + + // Possible implementation of partition + template + ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p){ + + first = std::find_if_not(first, last, p); + + // Condition to verify if first equals to last + if (first == last){ + return first; + } + + // Applying the condition + for (ForwardIt i = std::next(first); i != last; ++i){ + if (p(*i)){ + std::iter_swap(i, first); + ++first; + } + } + + return first; + } + + int main(){ + std::vector my_vector = { 2, 1, 5, 6, 8, 7 }; + + // Partitioning my_vector in basis of even numbers condition + std::partition(my_vector.begin(), my_vector.end(), [](int x){ + return x%2==0; + }); + + // Verifying if my_vector is partitioned + if ( std::is_partitioned(my_vector.begin(), my_vector.end(), [](int x){ return x%2==0; } ) ) { + std::cout << "The vector is partitioned!\n" << std::endl; + } else{ + std::cout << "The vector is not partitioned!\n" << std::endl; + } + + } + +``` + +**[Run Code](https://rextester.com/FGSWB8639)** \ No newline at end of file From 764e6c19bd491354360155a4b70b54f8b6080620 Mon Sep 17 00:00:00 2001 From: Bhupesh Varshney Date: Tue, 5 Nov 2019 18:50:06 +0530 Subject: [PATCH 2/3] Update Partition.md --- algorithm/Partition.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/algorithm/Partition.md b/algorithm/Partition.md index d06b100d..d30ded2a 100644 --- a/algorithm/Partition.md +++ b/algorithm/Partition.md @@ -2,6 +2,7 @@ **Description :** This function is used to partition the elements on basis of condition mentioned in its arguments. +**Example**: ```cpp // Possible implementation of partition @@ -45,4 +46,4 @@ ``` -**[Run Code](https://rextester.com/FGSWB8639)** \ No newline at end of file +**[Run Code](https://rextester.com/FGSWB8639)** From be4cc2bf65b0f921a3d53ce40cb177a3e57b3a68 Mon Sep 17 00:00:00 2001 From: Bhupesh Varshney Date: Tue, 5 Nov 2019 18:50:26 +0530 Subject: [PATCH 3/3] Rename Partition.md to partition.md --- algorithm/{Partition.md => partition.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename algorithm/{Partition.md => partition.md} (100%) diff --git a/algorithm/Partition.md b/algorithm/partition.md similarity index 100% rename from algorithm/Partition.md rename to algorithm/partition.md