我的 shell 排序看起来像这样:
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
将整数添加到迭代器。
关于c++ - 实现基于迭代器的 shell 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666209/