添加排序功能

实现:
数组 冒泡排序 选择排序 二叉排序
用于存储的二叉排序结构
master
caiyuzheng 2020-05-08 01:43:46 +08:00
parent e1ecc299bb
commit 8624d19415
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
//
// Created by 29019 on 2020/5/7.
//
#ifndef GENERAL_SORTER_H
#define GENERAL_SORTER_H
#include <stdint.h>
template <typename T>
class CompareHanlder{
typedef bool (*CompareFunc) (T data1,T data2,uint64_t len);
CompareFunc Handler();
public:
CompareFunc mHandler;
};
template <typename T>
int bubblesort(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;
}
}
}
}
template <typename T>
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;
}
}
}
}
#endif //GENERAL_SORTER_H