如何使阵列旋转更加高效?

发布于 2024-12-13 00:48:58 字数 860 浏览 2 评论 0原文

如何使圆形阵列旋转更加高效?我在这个 线程 中读到了有关出色排序算法的内容,但它不适用于我需要什么,因为数组末尾有空格被排序到中间。

旋转功能需要同时适用于左旋转和右旋转。并非数组的每个空间都会被填充。

void Quack::rotate(int r)
{
    if(r > 0) //if r is positive, rotate left
    {
        for(int i = 0; i < r; i++)
            items[(qBack + i) % qCapacity] = items[(qFront + i) % qCapacity];
            //move items in array
    }
    else if(r < 0) //if r is negative, rotate right
    {
        for(int i = 0; i < (r * -1); i++)
            items[(qFront - i - 1) % qCapacity] =
                items[(qBack - i - 1) % qCapacity];
            //move items in array
    }
    //if r = 0, nothing happens

    //rotate front and back by r
    qFront = (qFront + r) % qCapacity;
    qBack = (qBack + r) % qCapacity;
}

How can I make my circular array rotation more efficient? I read in this thread about an excellent sorting algorithm, but it won't work for what I need because there are spaces at the end of the array that get sorted into the middle.

The rotation function needs to work for both left and right rotation. Not every space of the array will be filled.

void Quack::rotate(int r)
{
    if(r > 0) //if r is positive, rotate left
    {
        for(int i = 0; i < r; i++)
            items[(qBack + i) % qCapacity] = items[(qFront + i) % qCapacity];
            //move items in array
    }
    else if(r < 0) //if r is negative, rotate right
    {
        for(int i = 0; i < (r * -1); i++)
            items[(qFront - i - 1) % qCapacity] =
                items[(qBack - i - 1) % qCapacity];
            //move items in array
    }
    //if r = 0, nothing happens

    //rotate front and back by r
    qFront = (qFront + r) % qCapacity;
    qBack = (qBack + r) % qCapacity;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归属感 2024-12-20 00:48:58

我没有使用过它,所以我不能保证它能满足您所需的一切。但您可能想考虑简单地用 std::rotate 函数替换这个函数体。

它应该已经得到了很好的优化,并且不太可能在您的应用程序中引入错误。

http://www.sgi.com/tech/stl/rotate.html

如果您需要优化建议,我建议避免所有模运算。它们可能需要除法,这是您可以在处理器上执行的最昂贵的操作之一。它们是思考如何实现目标的便捷方法,但 CPU 的执行成本可能非常高。

如果使用两个循环,则可以删除模运算符:一个从中间到结束,另一个从开始到中间。

但如果可以的话,看看是否可以完全避免进行轮换。如果您小心的话,您也许能够消除无意义的全数组遍历/复制操作。请参阅我对 OP 的评论,了解如何实现这一目标。

I haven't used it, so I can't promise it will do everything you need. But you might want to look into simply replacing this function body with the std::rotate function.

It should already be well optimized, and will be much less likely to introduce bugs into your application.

http://www.sgi.com/tech/stl/rotate.html

If you want suggestions for optimization though, I recommend avoiding all modulo operations. They may require a divide, which is one of the most expensive operations you can perform on your processor. They are a convenient way to think about how to accomplish your goal, but could be very costly for your CPU to execute.

You can remove your modulo operators if you use two loops: one from the middle to the end, and the other from the beginning to the middle.

But if you can, see if you can avoid doing the rotation altogether. If you are careful you might be able to eliminate pointless full-array traversal/copy operations. See my comment on the OP for how to accomplish this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文