合并排序的这种实现是否使用相互递归?(Does this implementation of merge sort use mutual recursion?)

编程入门 行业动态 更新时间:2024-10-10 06:22:47
合并排序的这种实现是否使用相互递归?(Does this implementation of merge sort use mutual recursion?)

这个mergeSort算法是否使用相互递归? 我意识到mergeSort调用merge函数并调用它自己( mergeSort ),但由于merge不调用mergeSort ,它不是相互递归而只是递归吗?

function mergeSort(arr) { // split array ... return merge(mergSort(firstHalf), mergeSort(secondHalf)); } function merge (array1, array2) { // merge both arrays ... return singleArray; }

Does this mergeSort algorithm use mutual recursion? I realize that mergeSort calls the merge function and it calls itself (mergeSort), but since merge does not call mergeSort is it not mutual recursion and simply just recursion?

function mergeSort(arr) { // split array ... return merge(mergSort(firstHalf), mergeSort(secondHalf)); } function merge (array1, array2) { // merge both arrays ... return singleArray; }

最满意答案

正确:这是简单的递归。 相互递归也称为间接递归:A调用B; B打电话给A.

你的分析是完全正确的: merge调用mergeSort , 然后你会有相互递归。 在发布的代码中,对merge的调用是调用树上的简单父子链接。

Correct: this is simple recursion. Mutual recursion is also called indirect recursion: A calls B; B calls A.

Your analysis is exactly correct: were merge to call mergeSort, then you would have mutual recursion. In the posted code, the call to merge is a simple parent-child link on the call tree.

更多推荐

本文发布于:2023-08-06 17:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   implementation   merge   recursion   mutual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!