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

Commit

Permalink
Merge pull request #195 from dhiaeddineTounakti/push_heap
Browse files Browse the repository at this point in the history
added push_heap function
  • Loading branch information
Bhupesh-V authored Nov 5, 2019
2 parents cb30f43 + 0db027e commit 0552650
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions algorithm/push_heap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# push_heap

**Description** : Given a heap in the range [first,last-1), this function extends the range considered a heap to [first,last) by placing the value in (last-1) into its corresponding location within it.

**Example**:
```cpp
#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector

int main () {
int myints[] = {10,20,30,5,15};
std::vector<int> v(myints,myints+5);

std::make_heap (v.begin(),v.end());
std::cout << "initial max heap : " << v.front() << '\n';

std::pop_heap (v.begin(),v.end()); v.pop_back();
std::cout << "max heap after pop : " << v.front() << '\n';

v.push_back(99); std::push_heap (v.begin(),v.end());
std::cout << "max heap after push: " << v.front() << '\n';

std::sort_heap (v.begin(),v.end());

std::cout << "final sorted range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];

std::cout << '\n';

return 0;
}
```

**[Run Code](https://rextester.com/WWHJ67453)**

0 comments on commit 0552650

Please sign in to comment.