diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-01-15 22:31:40 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-01-15 22:31:40 +0100 |
commit | 5d7f508c6241ed8031b220595c9ceb67e32e164d (patch) | |
tree | 7ccfcfdcec672280d7e1fc8ce5815130f67d6c54 /Year_2/Algorithms | |
parent | bfb80b55de097bf140b900fad5c72125f216a906 (diff) |
Fix compare for heapsort
Diffstat (limited to 'Year_2/Algorithms')
-rw-r--r-- | Year_2/Algorithms/heapsort.cc | 9 |
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 }; }; |