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
|
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)
|
set(COPYITEM inc)
|
||||||
file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*)
|
file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*)
|
||||||
|
|
|
@ -5,12 +5,28 @@
|
||||||
#ifndef GENERAL_SORTER_H
|
#ifndef GENERAL_SORTER_H
|
||||||
#define GENERAL_SORTER_H
|
#define GENERAL_SORTER_H
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class CompareHanlder{
|
class CompareHanlder{
|
||||||
typedef bool (*CompareFunc) (T data1,T data2,uint64_t len);
|
typedef bool (*CompareFunc) (T data1,T data2);
|
||||||
CompareFunc Handler();
|
|
||||||
public:
|
public:
|
||||||
|
CompareFunc Handler(){return mHandler;};
|
||||||
CompareFunc 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>
|
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 i = 0;i < len - 1;i++){
|
||||||
for(uint64_t z = 0; z < len - 1 - i;z++){
|
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];
|
T tmp = data[z + 1];
|
||||||
data[z + 1] = data[z];
|
data[z + 1] = data[z];
|
||||||
data[z] = tmp;
|
data[z] = tmp;
|
||||||
|
@ -34,60 +50,45 @@ int selectsort(T *data,uint64_t len,CompareHanlder<T> handle){
|
||||||
if(nullptr == data){
|
if(nullptr == data){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for(uint64_t i = 0;i < len - 1;i++){
|
for(uint64_t i = 0;i < len;i++){
|
||||||
for(uint64_t z = 0; z < len - 1 - i;z++){
|
T biggestindex = i;
|
||||||
if(handle.Handler(data[z],data[z + 1],len)){
|
for(uint64_t z = i; z < len ;z++){
|
||||||
T tmp = data[z + 1];
|
if(handle.Handler()(data[z],data[biggestindex])){
|
||||||
data[z + 1] = data[z];
|
biggestindex = z;
|
||||||
data[z] = tmp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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>
|
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)的位置
|
if(!tree){ //找到插入位置
|
||||||
int parent = (current - 1)/2; // 父(parent)结点的位置
|
BSTNode<T> *leaf = new BSTNode<T>; //新节点保存数据
|
||||||
int tmp = heapptr[current]; // 当前节点(current)的大小
|
leaf->data = data;
|
||||||
|
leaf->lchild = leaf->rchild = NULL;
|
||||||
while(current > 0)
|
tree = leaf; //把新结点放到已找到的插入位置
|
||||||
{
|
}
|
||||||
// 父节点大于当前节点
|
else if(data < tree->data.key)
|
||||||
if(heapptr[parent] >= tmp)
|
InsertBST(tree->lchild,data);
|
||||||
break;
|
else if(data > tree->data.key)
|
||||||
else
|
InsertBST(tree->rchild,data);
|
||||||
{ // 父节点小于当前节点,就调换位置,当前节点作为父节点,来源于一个抢位置的概念
|
else{
|
||||||
heapptr[current] = heapptr[parent];
|
cout<<"已有此值~"<<endl;
|
||||||
current = parent;
|
return;
|
||||||
parent = (parent - 1)/2; // 再把当前节点和上一个父节点在比较大小
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 找到合适的索引
|
|
||||||
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>
|
template <typename T>
|
||||||
int heapsort(T *data,uint64_t len,CompareHanlder<T> handle){
|
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
|
#endif //GENERAL_SORTER_H
|
||||||
|
|
Loading…
Reference in New Issue