实现基于迭代器的 shell 排序
Implementing a iterator based shell sort
我的外壳排序如下所示:
template<class T>
void shellSort(T *begin, T *end) {
int shell = 1;
while (shell < (begin - end) / 3) shell = shell * 3 + 1;
while (shell > 0) {
for (auto index = shell; index < end; index++) {
for (auto insertion = index; insertion >= shell && *(insertion - shell) > *(insertion); insertion -= shell) {
swap(*(insertion - shell), *(insertion));
}
}
shell = shell / 3;
}
}
磨坊的漂亮运行。我遇到的问题出在这一行:
for (auto index = shell; index < end; index++)
由于shell
是一个int
但end
是一个int *
它不知道如何进行比较。我该如何解决这个问题?
假设这些是随机访问迭代器,否则性能会很差。
您可以使用std::distance
来获取两个迭代器之间的差异。还可以使用 std::advance
向迭代器添加整数。
使用"迭代器"对项目进行寻址,仅对相对偏移量使用整数:
for (auto index = begin + shell; index < end; ++index) ...
顺便说一句,您可能想要shell < (end - begin)/3
,而不是(begin - end)
.
相关文章:
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 如何通过cpp程序运行shell脚本
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- shell排序中的交换和比较
- 使用 c++ 中的线程进行 shell 排序
- 在C 中实现Shell排序序列
- 了解C++Shell排序
- 实现基于迭代器的 shell 排序
- shell将伪代码排序为c++代码