parent
e1ecc299bb
commit
8624d19415
|
@ -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
|
Loading…
Reference in New Issue