快速排序(模板)
来源:互联网 发布:mac系统的下载栏不见了 编辑:程序博客网 时间:2024/05/17 03:58
/*
* 说明:经过本函数处理后,数组ItemArray[]中元素满足pRuleFunc(ItemArray[ItemR], ItemArray[ItemL])为True, 其中ItemR 〉ItemL;
*/
template T;
void QuickSort(ItemArray[], int ItemL, int ItemR, bool(*pRuleFunc(T, T))
{
if (ItemL >= ItemR)
{
return;
}
int iL = ItemL;
int iR = ItemR;
int ItemK = ItemArray[iL];
for ( ;iL < iR;)
{
// 从右向左寻找第一个使得pRuleFunc(ItemArray[iR], ItemK)为false的元素,并将其移动到ItemK的左侧
while((iL < iR) && pRuleFunc(ItemArray[iR], ItemK))
{
iR--;
}
ItemArray[iL] = ItemArray[iR];
// 从左向右寻找第一个使得pRuleFunc(ItemK, ItemArray[iL])为false的元素,并将其移动到ItemK的右侧
while((iL < iR) && pRuleFunc(ItemK,ItemArray[iL]))
{
iL++;
}
ItemArray[iR] = ItemArray[iL];
}
ItemArray[iL] = ItemK;
// 快速排序左侧,
QuickSort(ItemArray, ItemL, iL - 1, pRuleFunc);
// 快速排序右侧,
QuickSort(ItemArray, iL + 1, ItemR, pRuleFunc);
}
1。用例-数字排序
bool pIntCmp(int i, int j)
{
if (i < j)
{
return true;
}
else
{
return false;
}
}
int main(int argc, char* argv[])
{
int aTest[10] = {0, 1, 3, 4, 6, 9, 8, 7, 5, 2};
QuickSort<int>(aTest, 0, 9, pIntCmp);
return 0;
}
2。用例-字符串排序
bool pStrCmp(char *a, char*b)
{
if (NULL = a)
return false;
if (NULL == b)
return true;
if (strcmp(a, b) > 0)
return true;
else
return false;
}
int main(int argc, char* argv[])
{
char * bTest[10] = {"abcd", "hehe", "come on", "baby", "oh no", "haha", "how are you", "time out", "the second", "the first"};
QuickSort<char*>(bTest, 0, 9, pStrCmp);
return 0;
}
- 【排序】快速排序模板
- 【排序】快速排序模板
- 快速排序模板
- 快速排序模板
- C++快速排序模板
- C++快速排序模板
- 快速排序(模板)
- 快速排序【模板】
- 快速排序模板
- 快速排序----模板实现
- 快速排序函数模板
- 快速排序模板
- 快速排序【模板】
- 【模板】快速排序
- P1177 【模板】快速排序
- 【模板】快速排序
- 快速排序模板
- 洛谷 [模板]快速排序
- OpenCV参考手册之Mat类详解(一)
- 平面上N个点,每两个点都确定一条直线, 求出斜率最大的那条直线所通过的两个点
- 森林与二叉树的转换 (转)
- 贝叶斯分类在邮件分类
- 网络子系统60_路由子系统初始化蓝图
- 快速排序(模板)
- python 统计 英文 单词
- Java 如何在Main函数中,执行完异步任务后才退出主线程
- 数组中 &a和 &a[0],a 的区别
- 创业浪中回归真实的商业逻辑
- oracle 导出表、方案、数据库和他们的导入
- 智力游戏——五海盗分赃
- 机器学习领域一些值得关注的人
- RJ45接口