C++ 快排
来源:互联网 发布:时间序列数据集 编辑:程序博客网 时间:2024/06/17 06:00
#include <iostream>
void swap(int *beg, int *end);
int * Partition(int *beg, int *end);
void QSort(int *beg, int *end);
int main()
{
int arr[7] = { 3, 9, 8, 2, 3, 5, 6 };
int *beg = std::begin(arr);
int *end = std::end(arr) - 1;//end(), 返回尾后迭代器
QSort(beg, end);
while (beg <= end)
{
std::cout << *beg++ << std::endl;
}
system("pause");
return 0;
}
int * Partition(int *beg, int *end)
{
int pivot = *beg;
while (beg < end)
{
while (beg < end && *end >= pivot)//从右向左寻找关键字小于pivot的值
{
--end;
}
swap(end, beg);
while (beg < end && *beg <= pivot)//从左向右寻找关键字大于等于pivot的值
{
++beg;
}
swap(beg, end);
}
return beg;
}
void swap(int *beg, int *end)
{
int tmp = *beg;
*beg = *end;
*end = tmp;
}
void QSort(int *beg, int *end)
{
if (beg < end)
{
int *pivotLoc = Partition(beg, end);
QSort(beg, pivotLoc - 1);
QSort(pivotLoc + 1, end);
}
}
void swap(int *beg, int *end);
int * Partition(int *beg, int *end);
void QSort(int *beg, int *end);
int main()
{
int arr[7] = { 3, 9, 8, 2, 3, 5, 6 };
int *beg = std::begin(arr);
int *end = std::end(arr) - 1;//end(), 返回尾后迭代器
QSort(beg, end);
while (beg <= end)
{
std::cout << *beg++ << std::endl;
}
system("pause");
return 0;
}
int * Partition(int *beg, int *end)
{
int pivot = *beg;
while (beg < end)
{
while (beg < end && *end >= pivot)//从右向左寻找关键字小于pivot的值
{
--end;
}
swap(end, beg);
while (beg < end && *beg <= pivot)//从左向右寻找关键字大于等于pivot的值
{
++beg;
}
swap(beg, end);
}
return beg;
}
void swap(int *beg, int *end)
{
int tmp = *beg;
*beg = *end;
*end = tmp;
}
void QSort(int *beg, int *end)
{
if (beg < end)
{
int *pivotLoc = Partition(beg, end);
QSort(beg, pivotLoc - 1);
QSort(pivotLoc + 1, end);
}
}
阅读全文
0 0
- 快排---C语言
- 快排 c
- C实现快排算法
- 快排 c语言实现
- C语言 快排函数
- C语言实现快排
- C语言快排实现
- 快排C语言实现
- C/C++调用快排的模板
- 快排(库函数实现)C语言
- 快排c代码实现
- 各种快排调用模板 c语言版
- C语言中的qsort快排库函数
- C/C++调用快排的模板
- 快排(C语言实现)
- 算法导论快排实现(C++)
- C语言快排函数qsort()
- C语言.冒泡选择及快排
- SPI机制的一次具体使用
- 挑战程序竞赛系列(33):POJ 2991 Crane
- [USACO3.3]亚瑟王的宫殿 Camelot
- Android apk 加固
- 线程阻塞怎么结束
- C++ 快排
- java的path与classpath
- Mysql笔记
- Pandas学习总结(上)
- 常见类---BigDecimal类
- 设计模式之单例模式
- (noip 模拟 染色)<树形DP>
- Prime Factors
- nspr线程相关