From 5d7f508c6241ed8031b220595c9ceb67e32e164d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 15 Jan 2023 22:31:40 +0100 Subject: Fix compare for heapsort --- Year_2/Algorithms/heapsort.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'Year_2') diff --git a/Year_2/Algorithms/heapsort.cc b/Year_2/Algorithms/heapsort.cc index 1709be5..d5108aa 100644 --- a/Year_2/Algorithms/heapsort.cc +++ b/Year_2/Algorithms/heapsort.cc @@ -35,9 +35,9 @@ public: int r = right(i); int max = i; - if (l <= heapsize_ && !compare(l, i)) + if (l <= heapsize_ && compare(l, max)) max = l; - if (r <= heapsize_ && !compare(r, max)) + if (r <= heapsize_ && compare(r, max)) max = r; if (max != i) { @@ -102,10 +102,11 @@ private: int heapsize_ { 0 }; int n_; HeapType type_; - int + + bool compare(int i, int j) { - return (type_ == HeapType::Min) ? a_[i] < a_[j] : a_[j] > a_[i]; + return (type_ == HeapType::Max) ? a_[i] > a_[j] : a_[j] > a_[i]; } int count_ { 0 }; }; -- cgit v1.2.3-18-g5258