简体   繁体   English

我是否正确理解此递归?

[英]Am I correctly understanding this recursion?

Could someone please help walk me through my recursive code? 有人可以帮我浏览我的递归代码吗? Here's how I make sense of it (but I do not think I am stepping through the code correctly): 这就是我的理解方式(但我认为我没有正确地逐步执行代码):

  1. if ( first > last ) return -1
  2. else 其他
  3. if ( result == 0 ) return last
  4. else return SeqSearch (data, first, last-1, key)
  5. restart the method but with last as last-1 ("keller") 重新启动该方法,但lastlast-1 (“ keller”)
  6. repeat steps 1, 2, and 3 重复步骤1、2和3
  7. else return SeqSearch(data, first, last-1, key)
  8. restart the method but with last as last-1 ("six") 重新启动方法,但lastlast-1 (“ six”)
  9. etc ... 等...

Here is my code: 这是我的代码:

public static void main (String[] args)
{
    String[] data = new String[]{"help","jackson","six","keller","mean"};
    int first = 0;
    int last = data.length-1;
    String key ="help";
    System.out.println(SeqSearch(data,first,last,key));
}
public static int SeqSearch(String[] data,int first,int last,String key)
{
    if(first > last)
        return -1;
    else{
        int result = data[last].compareTo(key);
        if(result == 0)
            return last;
        else
            return SeqSearch(data,first,last-1,key);
    }
}

A good way to understand a recursive function is to break it down into its base cases and recursive cases. 理解递归函数的一个好方法是将其分解为基本案例和递归案例。

This SeqSearch has two base cases: SeqSearch有两种基本情况:

1. Not found 1.找不到

if (first > last)
    return -1;

2. Value found 2.发现价值

if (data[last].compareTo(key) == 0)
    return last;

Now, that leaves the recursive cases. 现在,剩下了递归的情况。 Here, we have only one recursive case, but there could be several. 在这里,我们只有一个递归的情况,但是可能有几个。

Now, when designing a recursive function, you have to ensure that each recursive call is reduced , or simpler , than previous calls, in the sense that we're getting closer to one of the base cases each time. 现在,在设计递归函数时,必须确保每个递归调用都比以前的调用减少简化 ,这意味着每次我们都接近一种基本情况。 This is very much related to the mathematical concept of Induction . 这与归纳的数学概念非常相关。

So, each call to the recursive case must progress one "step" closer to the answer. 因此,对递归案例的每次调用都必须比答案更近一步。 * Here we see that the value of last is reduced by subtracting one, bringing it closer to zero at each step. *在这里,我们看到last的值通过减去1来减小,在每一步将其接近零。

In turn, this means that the function is referring to a smaller and smaller subset of the data array; 反过来,这意味着函数正在引用data数组中越来越小的子集。 conceptually, this is analogous to passing a smaller array to the recursive call, which has one less element. 从概念上讲,这类似于将较小的数组传递给递归调用,该递归调用具有较少的元素。

At this point, the base cases start to make sense: 至此,基本案例开始变得有意义了:

  1. When first is greater than last , we have an array with no elements: the tail of the list has overtaken its head . first大于last ,我们有一个没有元素的数组:列表的尾部超过了head

  2. When the search key is found, we return its index as our result. 找到搜索键后,我们返回其索引作为结果。

This function is peculiar (among search functions), in that it finds the first matching index from the end of the list; 此功能是特殊功能(在搜索功能中),因为它从列表的末尾查找第一个匹配的索引。 a more commonplace operation is to find the first matching index from the beginning of a list. 更常见的操作是从列表的开头查找第一个匹配的索引。

This could be achieved by incrementing first instead of decrementing last . 这可以通过first递增而不是last递减来实现。 This would still count as a reduction --- even though it is adding --- because the recursive step is strictly smaller than the original step. 尽管递归步骤严格小于原始步骤,但这仍然算作减少-即使添加了-。


* This means that each recursive call much be "simpler" than the previous one; *这意味着每个递归调用都比上一个“简单”; so if you can understand the problem at any point, the next step should be simpler; 因此,如果您可以随时理解问题,则下一步应该更简单; the only complication is that it is nested within the original step. 唯一的麻烦是它嵌套在原始步骤中。

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

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