fix: HeapSort没有虚拟析构函数
This commit is contained in:
@@ -21,23 +21,23 @@ namespace ling {
|
|||||||
protected:
|
protected:
|
||||||
virtual bool isBig(const T &, const T &) const = 0;
|
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();
|
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();
|
return right(i) < list->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] inline int parent(int i) {
|
[[nodiscard]] static inline int parent(const int i) {
|
||||||
return (i - 1) / 2;
|
return (i - 1) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] inline int left(int i) const {
|
[[nodiscard]] static inline int left(const int i) {
|
||||||
return i * 2 + 1;
|
return i * 2 + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] inline int right(int i) const {
|
[[nodiscard]] static inline int right(const int i) {
|
||||||
return i * 2 + 2;
|
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) {
|
while (true) {
|
||||||
int largest = i;
|
int largest = i;
|
||||||
int l = left(i);
|
int l = left(i);
|
||||||
@@ -72,11 +72,13 @@ namespace ling {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void bottom(int i) {
|
void bottom(int i) {
|
||||||
int size = list->size();
|
const int size = list->size();
|
||||||
bottom(i, size);
|
bottom(i, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
virtual ~HeapSort() = default;
|
||||||
|
|
||||||
explicit HeapSort(std::vector<T> &list) {
|
explicit HeapSort(std::vector<T> &list) {
|
||||||
this->list = &list;
|
this->list = &list;
|
||||||
if (this->list->size() <= 1)
|
if (this->list->size() <= 1)
|
||||||
@@ -84,7 +86,7 @@ namespace ling {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
for (int i = parent(list->size() - 1); i >= 0; i--) {
|
for (int i = parent(list->size() - 1); i >= 0; --i) {
|
||||||
bottom(i);
|
bottom(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +98,7 @@ namespace ling {
|
|||||||
|
|
||||||
void sort() {
|
void sort() {
|
||||||
int n = list->size();
|
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]);
|
std::swap((*list)[0], (*list)[i]);
|
||||||
bottom(0, i);
|
bottom(0, i);
|
||||||
}
|
}
|
||||||
@@ -105,7 +107,7 @@ namespace ling {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class HeapSortDefault : public HeapSort<T> {
|
class HeapSortDefault final : public HeapSort<T> {
|
||||||
protected:
|
protected:
|
||||||
bool isBig(const T &t, const T &t1) const override {
|
bool isBig(const T &t, const T &t1) const override {
|
||||||
return t > t1;
|
return t > t1;
|
||||||
|
|||||||
Reference in New Issue
Block a user