归并 - 是自底向上的速度比自顶向下?速度

2023-09-11 03:41:02 作者:十四次野战

我一直在读算法,第4版,由塞奇威克和放大器;韦恩,一路上我一直在用JavaScript实现讨论的算法。

I've been reading "Algorithms, 4th Ed" by Sedgewick & Wayne, and along the way I've been implementing the algorithms discussed in JavaScript.

最近我在书中比较自上而下和自下而上的方法提供的归并例子......但我发现,自底向上的运行速度更快(我认为)。看我的分析,我的博客上。    - 的http://www.akawebdesign.com/2012/04/13/javascript-mergesort-top-down-vs-bottom-up/

I recently took the mergesort examples provided in the book to compare top-down and bottom-up approaches... but I'm finding that bottom-up is running faster (I think). See my analysis on my blog. - http://www.akawebdesign.com/2012/04/13/javascript-mergesort-top-down-vs-bottom-up/

我一直没能找到任何讨论,说归并的一种方法应该比其他的快。是我实现(或分析)有缺陷?

I have not been able to find any discussion that says one method of mergesort should be faster than the other. Is my implementation (or analysis) flawed?

注:我的分析测量算法的迭代循环,没有严格的数组比较/移动。也许,这是有缺陷的或不相关?

Note: my analysis measures the iterative loops of the algorithm, not strictly the array compares/moves. Perhaps this is flawed or irrelevant?

编辑:我的分析,实际上并没有时间的速度,所以我对此声明运行快是有点误导我通过递归方法跟踪迭代(顶级向下)和用于环(自下而上) - 和自下而上似乎使用更少的迭代次数

My analysis didn't actually time the speed, so my statement about it running "faster" is a bit misleading. I am tracking the "iterations" through the recursive method (top-down) and the for loops (bottom-up) - and bottom-up appears to use fewer iterations.

推荐答案

如果由你越快意味着更少的反复,那么肯定。如果你想知道关于执行时间可能。

If by faster you mean fewer "iterations" then yes. If you're wondering about execution time maybe.

原因是一些人​​21513迭代是做多了22527次迭代。

The reason is some of those 21,513 iterations are doing more than the 22,527 iterations.

从查看源似乎像一些在你的图中的叶节点都被排序在一起不单独产生更少的合并和排序,但他们需要更长的时间。

From looking at the source it seems like some of the leaf nodes in your diagram are being sorted together not individually resulting in fewer merges and sorts but them taking longer.