排序算法模板,包括快速排序,希尔排序,冒泡排序
快速排序:以数据为基准的左右二分排序
希尔排序:缩小增量的排序
需要使用可以直接下载文件使用
/*
*file : sort.h
*author : oliver ou
*/
#ifndef _AGILE_SORT_H_
#define _AGILE_SORT_H_
#include <iostream>
#include <vector>
//比较函数,返回较大的那个
typedef void* (*GetGreater)(void*, void*);
//排序类
template<typename Type>
class AgileSort
{
public:
~AgileSort()
{
};
//单例调用 AgileSort<Class>::Instance().QiuckSortAsc(…)
static AgileSort& Instance()
{
static AgileSort<Type> instance;
return instance;
}
//快速排序,升序
void QiuckSortAsc(std::vector<Type>& data, GetGreater func = NULL)
{
size_t start = 0;
QiuckSort(data, start, data.size() – 1, true, func);
};
//快速排序,降序
void QiuckSortDes(std::vector<Type>& data, GetGreater func = NULL)
{
size_t start = 0;
QiuckSort(data, start, data.size() – 1, false , func);
};
//希尔排序,升序
void ShellSortAsc(std::vector<Type>& data, size_t interval, GetGreater func = NULL)
{
while (interval > 0)
{
ShellSortHelp(data, interval–, true, func);
}
};
//希尔排序,降序
void ShellSortDes(std::vector<Type>& data, size_t interval, GetGreater func = NULL)
{
while (interval > 0)
{
ShellSortHelp(data, interval–, false, func);
}
};
//冒泡排序,升序
void BubbleSortAsc(std::vector<Type>& data, GetGreater func = NULL)
{
BubbleSort(data, true, func);
};
//冒泡排序,降序
void BubbleSortDes(std::vector<Type>& data, GetGreater func = NULL)
{
BubbleSort(data, false, func);
};
private:
AgileSort()
{
};
//交换
void Swap(std::vector<Type>& data, size_t i, size_t j)
{
Type temp = data[i];
data[i] = data[j];
data[j] = temp;
};
//快速排序递归
void QiuckSort(std::vector<Type>& data, size_t& start, size_t end, bool isAscend, GetGreater func = NULL)
{
if (start >= end || start >= data.size() || end >= data.size() )
{
return;
}
size_t left = start;
QiuckSortHelp(data, start, end, isAscend, func);
size_t right = start + 1;
QiuckSort(data, left, start – 1, isAscend, func);
QiuckSort(data, right, end, isAscend, func);
};
void QiuckSortHelp(std::vector<Type>& data, size_t& start, size_t end, bool isAscend, GetGreater func = NULL)
{
size_t i = start + 1;
while (i <= end)
{
if (func)
{
if (isAscend)
{
if (&data[i] == (*func)(&data[start], &data[i]))
{
Swap(data, start++, start);
if (start != i)
{
Swap(data, start – 1, i);
}
}
}
else
{
if (&data[start] == (*func)(&data[start], &data[i]))
{
Swap(data, start++, start);
if (start != i)
{
Swap(data, start – 1, i);
}
}
}
}
else
{
if (isAscend)
{
if (data[start] > data[i])
{
Swap(data, start++, start);
if (start != i)
{
Swap(data, start – 1, i);
}
}
}
else
{
if (data[start] < data[i])
{
Swap(data, start++, start);
if (start != i)
{
Swap(data, start – 1, i);
}
}
}
}
i++;
}
};
void ShellSortHelp(std::vector<Type>& data, size_t interval, bool isAscend, GetGreater func = NULL)
{
int size = (int)data.size();
int loopSize = size / (int)interval;
int i = 0;
while (i < loopSize)
{
int end = i + (size – i – 1) / interval * interval;
int m = i;
int n = end;
while (m < end)
{
n = end;
while (n > m)
{
int next = n – interval;
if (next >= i)
{
if (func)
{
if (isAscend)
{
if (&data[n] == (*func)(&data[next], &data[n]))
{
Swap(data, next, n);
}
}
else
{
if (&data[next] == (*func)(&data[next], &data[n]))
{
Swap(data, next, n);
}
}
}
else
{
if (isAscend)
{
if (data[next] > data[n])
{
Swap(data, next, n);
}
}
else
{
if (data[next] < data[n])
{
Swap(data, next, n);
}
}
}
}
n -= interval;
}
m += interval;
}
i++;
}
};
void BubbleSort(std::vector<Type>& data, bool isAscend, GetGreater func = NULL)
{
int size = (int)data.size() – 1;
int j = size;
for (int i = 0; i < size; i++)
{
j = size;
while (j > i )
{
int next = j – 1;
if (func)
{
if (isAscend)
{
if (&data[j] == (*func)(&data[next], &data[j]))
{
Swap(data, next, j);
}
}
else
{
if (&data[next] == (*func)(&data[next], &data[j]))
{
Swap(data, next, j);
}
}
}
else
{
if (isAscend)
{
if (data[next] > data[j])
{
Swap(data, next, j);
}
}
else
{
if (data[next] < data[j])
{
Swap(data, next, j);
}
}
}
j–;
}
}
};
};
#endif //_AGILE_SORT_H_