From 06dcb18dcb03804a1e9e3d876d5c713fa7e31ee3 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Sun, 10 May 2020 17:07:25 +0800 Subject: [PATCH] no message --- general/CMakeLists.txt | 2 +- general/src/algorithm/sorter.hpp | 93 ++++++++++++++++---------------- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/general/CMakeLists.txt b/general/CMakeLists.txt index 2c4e1e0..4e79081 100644 --- a/general/CMakeLists.txt +++ b/general/CMakeLists.txt @@ -35,7 +35,7 @@ aux_source_directory(src/pattern PaternSrc) add_library(General OBJECT ${DIRSRCS} ${PaternSrc} src/pattern/signleton.hpp src/pattern/Observer.hpp src/pattern/stratergy.hpp src/pattern/adapter.hpp src/encrypt/base64.cpp src/encrypt/base64.h src/encrypt/aes.cpp src/encrypt/aes.h src/encrypt/rsa.cpp - src/math/BigInt.hpp src/net/TcpClient.cpp src/net/TcpClient.h src/net/PackageReceiver.cpp src/net/PackageReceiver.h src/function/btree.cpp src/function/btree.h) + src/math/BigInt.hpp src/net/TcpClient.cpp src/net/TcpClient.h src/net/PackageReceiver.cpp src/net/PackageReceiver.h src/function/btree.cpp src/function/btree.h src/algorithm/sorter.hpp) set(COPYITEM inc) file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*) diff --git a/general/src/algorithm/sorter.hpp b/general/src/algorithm/sorter.hpp index 47039c8..9fdbae3 100644 --- a/general/src/algorithm/sorter.hpp +++ b/general/src/algorithm/sorter.hpp @@ -5,12 +5,28 @@ #ifndef GENERAL_SORTER_H #define GENERAL_SORTER_H #include +#include +using namespace std; template class CompareHanlder{ - typedef bool (*CompareFunc) (T data1,T data2,uint64_t len); - CompareFunc Handler(); + typedef bool (*CompareFunc) (T data1,T data2); public: + CompareFunc Handler(){return mHandler;}; CompareFunc mHandler; + CompareHanlder(CompareFunc func){ + mHandler = func; + } + +private: + CompareHanlder(){}; +}; + +template +class BSTNode { +public: + T data; //每个结点的数据域包括关键字项和其他信息 + struct BSTNode *lchild; + struct BSTNode *rchild; }; template @@ -20,7 +36,7 @@ int bubblesort(T *data,uint64_t len,CompareHanlder handle){ } for(uint64_t i = 0;i < len - 1;i++){ for(uint64_t z = 0; z < len - 1 - i;z++){ - if(handle.Handler(data[z],data[z + 1],len)){ + if(handle.Handler()(data[z],data[z + 1])){ T tmp = data[z + 1]; data[z + 1] = data[z]; data[z] = tmp; @@ -34,60 +50,45 @@ int selectsort(T *data,uint64_t len,CompareHanlder handle){ if(nullptr == data){ return -1; } - for(uint64_t i = 0;i < len - 1;i++){ - for(uint64_t z = 0; z < len - 1 - i;z++){ - if(handle.Handler(data[z],data[z + 1],len)){ - T tmp = data[z + 1]; - data[z + 1] = data[z]; - data[z] = tmp; + for(uint64_t i = 0;i < len;i++){ + T biggestindex = i; + for(uint64_t z = i; z < len ;z++){ + if(handle.Handler()(data[z],data[biggestindex])){ + biggestindex = z; } } + T tmp = data[biggestindex]; + data[biggestindex] = data[i]; + data[i] = tmp; } } -template -static void adjustHeap(T []arr,int i,uint32_t len) { - -} template -static void maxheap_filterup(T *heapptr,uint32_t pos,uint32_t len) +void InsertBST(BSTNode &tree,T data) { - uint32_t current = pos; // 当前节点(current)的位置 - int parent = (current - 1)/2; // 父(parent)结点的位置 - int tmp = heapptr[current]; // 当前节点(current)的大小 - - while(current > 0) - { - // 父节点大于当前节点 - if(heapptr[parent] >= tmp) - break; - else - { // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念 - heapptr[current] = heapptr[parent]; - current = parent; - parent = (parent - 1)/2; // 再把当前节点和上一个父节点在比较大小 - } + if(!tree){ //找到插入位置 + BSTNode *leaf = new BSTNode; //新节点保存数据 + leaf->data = data; + leaf->lchild = leaf->rchild = NULL; + tree = leaf; //把新结点放到已找到的插入位置 + } + else if(data < tree->data.key) + InsertBST(tree->lchild,data); + else if(data > tree->data.key) + InsertBST(tree->rchild,data); + else{ + cout<<"已有此值~"< -int heapinert(T *heapptr,T data,uint32_t pos,uint32_t len) -{ - // 如果"堆"已满,则返回 - if(pos == len) - return -1; - - m_heap[pos] = data; // 将"数组"插在表尾 - heapfilterup(pos); // 向上调整堆 - - return 0; } template int heapsort(T *data,uint64_t len,CompareHanlder handle){ - + for(uint32_t i=0; i