c++ - 实现基于迭代器的 shell 排序

标签 c++ iterator shellsort

我的 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 是一个 intend 是一个 int * 它不知道该怎么做比较。我该如何解决这个问题?

最佳答案

假设这些是随机访问迭代器,否则性能会很差。

您可以使用 std::distance 来获取两个迭代器之间的差异。您还可以使用 std::advance 将整数添加到迭代器。

关于c++ - 实现基于迭代器的 shell 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666209/

相关文章:

c++ - 限制 C++ 函数允许的抛出有什么好处?

c++ - 使用惰性迭代器进行C++过滤

c - 对链表进行壳排序时出现问题

c - 希尔排序的时间复杂度

c++ - 如何在 valgrind 中跟踪/捕获 "Warning: invalid file descriptor -1 in syscall close"

c++ - 在 Windows 中访问 Intel CPU I/O 寄存器的简便方法

c++ - 链接器错误让我头疼

python - 斐波那契数列python

python - yield 可以产生多个连续的生成器吗?

java - 希尔排序不对数组的第一个元素进行排序