From 44f10ff65c91a6b2ef23aec1620433a311b7bb3f Mon Sep 17 00:00:00 2001 From: TUNQRT <bauerovakar@gmail.com> Date: Tue, 18 Feb 2025 21:01:03 +0100 Subject: [PATCH] heap sort --- heap.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/heap.cpp b/heap.cpp index ad0526e..6429600 100644 --- a/heap.cpp +++ b/heap.cpp @@ -10,6 +10,7 @@ public: void print() const; bool empty() const; + static std::vector<int>& sort(std::vector<int>&); Heap(std::vector<int> v); private: std::vector<int> data; @@ -19,8 +20,13 @@ private: void dump(std::ofstream&, int) const; }; -Heap::Heap(std::vector<int> v): data(std::move(v)){ +std::vector<int>& Heap::sort(std::vector<int>& v){ + Heap h(v); + for(auto& i: v) i = h.pop(); + return v; +} +Heap::Heap(std::vector<int> v): data(std::move(v)){ for(int i = data.size()/2; i >= 0; i--){ bubble_down(i); } @@ -92,13 +98,17 @@ void Heap::print() const { int main() { - Heap h({9, 7, 6, 5, 4, 3, 2, 1, 0, -3, 8}); + Heap h({9, 7, 6, 5, 4, 3, 2, 1, 0, -3, 8}); + std::vector<int> v = {4,6,2,8,0,56,3,4,7,8,2,3,45,5,4,2,432,69,42}; + Heap::sort(v); + for(auto& i: v){ + std::cout<<i<<", "; + } // for (auto i : v) { // h.push(i); h.print(); // } - // while (!h.empty()){ // int i = h.pop(); // h.print();