heap sort
This commit is contained in:
parent
48464840ba
commit
44f10ff65c
16
heap.cpp
16
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();
|
||||
|
|
Loading…
Reference in New Issue