简体   繁体   English

无法理解非递归MergeSort算法

[英]Cannot understand the non-recursive MergeSort algorithm

I've been trying to understand the non-recursive MergeSort algorithm after recently coding the recursive version. 在最近对递归版本进行编码之后,我一直试图理解非递归MergeSort算法。 My AP book doesn't provide too much info or examples on this topic so I was hoping someone could help me clear things up a bit. 我的美联社书籍没有提供太多有关此主题的信息或示例,因此我希望有人可以帮助我解决一些问题。

What does my book mean by the following: "In a non-recursive mergeSort method. We divide the list into two equal-sized parts and sort each with the selection sort, then merge the two using an algorithm to be discussed in part B." 我的书的意思是:“在非递归的mergeSort方法中。我们将列表分为两个相等大小的部分,并用选择排序对每个部分进行排序,然后使用将在B部分中讨论的算法将两者合并。 “

Does one always divide an array into two pieces in a non-recursive mergeSort method (and then sort them accordingly), or are there cases like the recursive version where you keep dividing until you get to array.length of 2 or 1? 是否总是在非递归的mergeSort方法中将数组分为两部分(然后对它们进行相应的排序),还是像递归版本那样不断划分直到达到array.length为2或1的情况?

Book code: 图书代码:

void mergeSort (ArrayList <Integer> A, int first, int last){
   int mid;

   mid = (first + last) / 2;
   selectionSort (A, first, mid);
   selectionSort (A, mid+1, last);
   merge (A, first, mid, last);
 }

If you always divide the array into 2 and then sort how is this efficient? 如果始终将数组分成2个,然后排序,效率如何? What happens if your array contains thousands of values . 如果数组包含数千个值,会发生什么情况。 . . wouldn't recursive be better off as it sorts the values in smaller pieces? 递归会不会更好,因为它将值按较小的部分排序?

Book demonstration: 书籍示范:

在此处输入图片说明

From my understanding what the book means is that you can divide the array in half, sort each array using selection sort, and then merge these two halves using the merge algorithm (which would be the same as for recursive mergesort). 根据我的理解,这本书的意思是您可以将数组分成两半,使用选择排序对每个数组进行排序,然后使用合并算法(与递归合并排序相同)将这两部分合并。 It may only want to show how the merging works. 它可能只想显示合并的工作方式。

However this implementation is NOT a mergesort. 但是,此实现不是 mergesort。 It's asymptotic efficiency is going to be far worse than mergesort's O(n log(n)) as selection sort is O(n^2). 由于选择排序为O(n ^ 2),因此它的渐近效率将比mergesort的O(n log(n)) 得多。

But it is possible to do a mergesort without the use of recursion - you could use iterations. 但是也可以不使用递归就进行合并排序-您可以使用迭代。 See the implementation here . 此处查看实现。

I think this example is being used purely to demonstrate a single step of mergesort. 我认为此示例仅用于演示mergesort的单个步骤。 With the two halves being sorted with selection sort, this implementation has very little of the "flavor" of mergesort, and certainly won't have mergesort's asymptotic complexity guarantees. 通过选择选择对这两个部分进行排序,此实现几乎没有mergesort的“风味”,并且当然不会具有mergesort的渐近复杂性保证。

If you've already coded up a recursive mergesort, I don't think this example has much relevance to you. 如果您已经编写了一个递归合并排序,那么我认为该示例与您没有太大关系。 Actual non-recursive mergesort looks very different from this. 实际的非递归合并排序看起来与此完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM