summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-15 22:31:40 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-15 22:31:40 +0100
commit5d7f508c6241ed8031b220595c9ceb67e32e164d (patch)
tree7ccfcfdcec672280d7e1fc8ce5815130f67d6c54 /Year_2/Algorithms
parentbfb80b55de097bf140b900fad5c72125f216a906 (diff)
Fix compare for heapsort
Diffstat (limited to 'Year_2/Algorithms')
-rw-r--r--Year_2/Algorithms/heapsort.cc9
1 files changed, 5 insertions, 4 deletions
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 };
};