no message
parent
1a1bbf0748
commit
06dcb18dcb
|
@ -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/*)
|
||||
|
|
|
@ -5,12 +5,28 @@
|
|||
#ifndef GENERAL_SORTER_H
|
||||
#define GENERAL_SORTER_H
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
class BSTNode {
|
||||
public:
|
||||
T data; //每个结点的数据域包括关键字项和其他信息
|
||||
struct BSTNode *lchild;
|
||||
struct BSTNode *rchild;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
@ -20,7 +36,7 @@ int bubblesort(T *data,uint64_t len,CompareHanlder<T> 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<T> 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 <typename T>
|
||||
static void adjustHeap(T []arr,int i,uint32_t len) {
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void maxheap_filterup(T *heapptr,uint32_t pos,uint32_t len)
|
||||
void InsertBST(BSTNode<T> &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<T> *leaf = new BSTNode<T>; //新节点保存数据
|
||||
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<<"已有此值~"<<endl;
|
||||
return;
|
||||
}
|
||||
// 找到合适的索引
|
||||
heapptr[current] = tmp; //最后把设置值
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
int heapsort(T *data,uint64_t len,CompareHanlder<T> handle){
|
||||
|
||||
for(uint32_t i=0; i <len; i++)
|
||||
{
|
||||
printf("%d ", data[i]);
|
||||
// 先组二叉排序数
|
||||
heapinert(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //GENERAL_SORTER_H
|
||||
|
|
Loading…
Reference in New Issue