diff --git a/include/HeapSort.h b/include/HeapSort.h index 8659907..8feb474 100644 --- a/include/HeapSort.h +++ b/include/HeapSort.h @@ -21,23 +21,23 @@ namespace ling { protected: virtual bool isBig(const T &, const T &) const = 0; - [[nodiscard]] inline bool isLeft(int i) const { + [[nodiscard]] inline bool isLeft(const int i) const { return left(i) < list->size(); } - [[nodiscard]] inline bool isRight(int i) const { + [[nodiscard]] inline bool isRight(const int i) const { return right(i) < list->size(); } - [[nodiscard]] inline int parent(int i) { + [[nodiscard]] static inline int parent(const int i) { return (i - 1) / 2; } - [[nodiscard]] inline int left(int i) const { + [[nodiscard]] static inline int left(const int i) { return i * 2 + 1; } - [[nodiscard]] inline int right(int i) const { + [[nodiscard]] static inline int right(const int i) { return i * 2 + 2; } @@ -50,7 +50,7 @@ namespace ling { } /// 下滤 - void bottom(int i, int size) { + void bottom(int i, const int size) { while (true) { int largest = i; int l = left(i); @@ -72,11 +72,13 @@ namespace ling { } void bottom(int i) { - int size = list->size(); + const int size = list->size(); bottom(i, size); } public: + virtual ~HeapSort() = default; + explicit HeapSort(std::vector &list) { this->list = &list; if (this->list->size() <= 1) @@ -84,7 +86,7 @@ namespace ling { } void init() { - for (int i = parent(list->size() - 1); i >= 0; i--) { + for (int i = parent(list->size() - 1); i >= 0; --i) { bottom(i); } } @@ -96,7 +98,7 @@ namespace ling { void sort() { int n = list->size(); - for (int i = n - 1; i > 0; i--) { + for (int i = n - 1; i > 0; --i) { std::swap((*list)[0], (*list)[i]); bottom(0, i); } @@ -105,7 +107,7 @@ namespace ling { }; template - class HeapSortDefault : public HeapSort { + class HeapSortDefault final : public HeapSort { protected: bool isBig(const T &t, const T &t1) const override { return t > t1;