添加堆排序

This commit is contained in:
2024-06-30 22:03:12 +08:00
parent 20ecee3980
commit f0abee1463
3 changed files with 166 additions and 5 deletions

View File

@@ -1,6 +1,9 @@
#include <iostream>
#include <RedBlackTree.h>
#include <HeapSort.h>
#include <random>
#include <chrono>
#include <iomanip>
class RedBlackTree : public ling::RedBlackTree<int> {
protected:
@@ -13,13 +16,40 @@ protected:
}
};
#define DATA_SIZE 10000000
//开始一个统计点
#define StartStating(name) auto name = std::chrono::high_resolution_clock::now()
//结束统计
#define EndStating(name, text) do {auto __ling_end_time = std::chrono::high_resolution_clock::now(); \
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(__ling_end_time - name); \
std::cout << text << ": " << std::fixed << std::setprecision(6) <<((double)duration.count() / 1000000) << " 秒(" << duration.count() <<" 微秒)" << std::endl;\
}while(false)
int main() {
RedBlackTree tree;
std::random_device rd;
std::mt19937 gen(rd());
for (int i = 0; i < 400000; i++)
tree.insert(i);
std::vector<int> list;
list.reserve(DATA_SIZE);
std::uniform_int_distribution<> dis(1, DATA_SIZE * 10);
for (int i = 0; i < DATA_SIZE; i++) {
std::cout << "Max : " << tree.maximum()->value << std::endl;
list.push_back(dis(gen));
}
StartStating(start);
ling::HeapSortDefault sort(list);
sort.init();
sort.sort();
EndStating(start, "排序耗时");
for (int i = 1; i < list.size(); i++) {
if (list[i] < list[i - 1]) {
std::cout << "错误!" << std::endl;
return -1;
}
}
return 0;
}