Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Partition #198

Merged
merged 3 commits into from
Nov 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions algorithm/partition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Partition

**Description :** This function is used to partition the elements on basis of condition mentioned in its arguments.

**Example**:
```cpp

// Possible implementation of partition
template<class ForwardIt, class UnaryPredicate>
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<int> 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)**