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& sort(std::vector&); Heap(std::vector v); private: std::vector data; @@ -19,8 +20,13 @@ private: void dump(std::ofstream&, int) const; }; -Heap::Heap(std::vector v): data(std::move(v)){ +std::vector& Heap::sort(std::vector& v){ + Heap h(v); + for(auto& i: v) i = h.pop(); + return v; +} +Heap::Heap(std::vector 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 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<