Javascript中带/不带子例程的简单递归

Javascript中带/不带子例程的简单递归,javascript,recursion,Javascript,Recursion,我一直在研究如何在javascript中递归地找到数组的最大值。我先反复尝试了一下,当然效果不错。递归地,我想先用一个子程序试试,然后不用子程序。调用内部子程序的最佳方式是什么?我被绊倒的地方是我想查看索引内部,但目前我的子例程接受数组作为参数 function maxValue(arr) { var max = 0; for (var i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[

我一直在研究如何在javascript中递归地找到数组的最大值。我先反复尝试了一下,当然效果不错。递归地,我想先用一个子程序试试,然后不用子程序。调用内部子程序的最佳方式是什么?我被绊倒的地方是我想查看索引内部,但目前我的子例程接受数组作为参数

function maxValue(arr) {
  var max = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}

console.log(maxValue([2, 7, 8, 3, 1, 4])); //returns 8

function maxValueRecursive(arr) {
  var length = arr.length; //6
  var max = 0;


  function doSearch(arr) {
    if (arr.length === 1) {
      max = arr[0];
    } else { // true

      max = Math.max(arr[length - 1], arr[length - 2]);
      //max = Math.max(4, doSearch()) = 4.

    }
    return max;
  }

  return doSearch(arr);
}

console.log(maxValueRecursive([2, 7, 8, 3, 1, 4])) //returns 4
函数最大值(arr){
var max=0;
对于(变量i=0;i最大值){
max=arr[i];
}
}
返回最大值;
}
log(最大值([2,7,8,3,1,4])//返回8
函数maxValueRecursive(arr){
var length=arr.length;//6
var max=0;
功能数据搜索(arr){
如果(arr.length==1){
max=arr[0];
}否则{//true
max=Math.max(arr[length-1],arr[length-2]);
//max=Math.max(4,doSearch())=4。
}
返回最大值;
}
返回数据搜索(arr);
}
log(maxValueRecursive([2,7,8,3,1,4])//返回4

我知道一开始感觉很奇怪,但从函数本身调用函数就像。。。好吧,从它的内部来称呼它

下面的函数将数组作为第一个参数,接下来的两个参数是搜索中该点的最大值的索引,以及分别比较最大值的下一个值。首先使用0和1,然后让函数从那里接管

var findMax = function (array, curMaxIndex, nextIndex) {
    if (array[nextIndex] > array[curMaxIndex]) {
        curMaxIndex = nextIndex;
    }
    if (nextIndex > array.length) {
        return array[curMaxIndex];
    }
    return findMax(array, curMaxIndex, nextIndex + 1);
}

var max = findMax([2, 7, 8, 3, 1, 4, 9], 0, 1);

console.log(max); //9
您还可以设置默认值,因此原始调用更简单:

var findMax = function (array, curMaxIndex, nextIndex) {
    curMaxIndex = typeof curMaxIndex !== 'undefined' ? curMaxIndex : 0;
    nextIndex = typeof nextIndex !== 'undefined' ? nextIndex : 1;
    if (array[nextIndex] > array[curMaxIndex]) {
        curMaxIndex = nextIndex;
    }
    if (nextIndex > array.length) {
        return curMaxIndex;
    }
    return findMax(array, curMaxIndex, nextIndex + 1);
}

var max = findMaxIndex(array);

不过,最好的方法是使用内置的
Math.max()
,您似乎已经听说过,所以我假设这只是一个学习练习

我知道一开始感觉很奇怪,但从函数本身调用函数就像。。。好吧,从它的内部来称呼它

下面的函数将数组作为第一个参数,接下来的两个参数是搜索中该点的最大值的索引,以及分别比较最大值的下一个值。首先使用0和1,然后让函数从那里接管

var findMax = function (array, curMaxIndex, nextIndex) {
    if (array[nextIndex] > array[curMaxIndex]) {
        curMaxIndex = nextIndex;
    }
    if (nextIndex > array.length) {
        return array[curMaxIndex];
    }
    return findMax(array, curMaxIndex, nextIndex + 1);
}

var max = findMax([2, 7, 8, 3, 1, 4, 9], 0, 1);

console.log(max); //9
您还可以设置默认值,因此原始调用更简单:

var findMax = function (array, curMaxIndex, nextIndex) {
    curMaxIndex = typeof curMaxIndex !== 'undefined' ? curMaxIndex : 0;
    nextIndex = typeof nextIndex !== 'undefined' ? nextIndex : 1;
    if (array[nextIndex] > array[curMaxIndex]) {
        curMaxIndex = nextIndex;
    }
    if (nextIndex > array.length) {
        return curMaxIndex;
    }
    return findMax(array, curMaxIndex, nextIndex + 1);
}

var max = findMaxIndex(array);

不过,最好的方法是使用内置的
Math.max()
,您似乎已经听说过,所以我假设这只是一个学习练习

您可以使用
Math.max
并在每一步求解数组中的较小位。诀窍是从数组中删除一些(任何)项,并使用
Math.max
将该项与较小数组中的
findmax
进行比较:

function findmax(arr){
    if (arr.length == 1){
        // base case - single item in array is always max
        return arr[0];
    }
    // recursive call on smaller problem (shorter array)
    return Math.max(arr[0], findmax(arr.slice(1)))
}
我使用了
slice
,但您可以像我一样使用
pop
或任何方法删除项目并使用
Math.max
对其进行比较

对于数组
[1,4,2,3]
递归展开如下:

1. findmax([1, 4, 2, 3]
2. Math.max(1, findmax([4, 2, 3]))
3. Math.max(1, Math.max(4, findmax([2, 3])))
4. Math.max(1, Math.max(4, Math.max(2, findmax([3]))))
5. Math.max(1, Math.max(4, Math.max(2, 3))) // 4

您可以使用
Math.max
并在每一步求解数组中较小的位。诀窍是从数组中删除一些(任何)项,并使用
Math.max
将该项与较小数组中的
findmax
进行比较:

function findmax(arr){
    if (arr.length == 1){
        // base case - single item in array is always max
        return arr[0];
    }
    // recursive call on smaller problem (shorter array)
    return Math.max(arr[0], findmax(arr.slice(1)))
}
我使用了
slice
,但您可以像我一样使用
pop
或任何方法删除项目并使用
Math.max
对其进行比较

对于数组
[1,4,2,3]
递归展开如下:

1. findmax([1, 4, 2, 3]
2. Math.max(1, findmax([4, 2, 3]))
3. Math.max(1, Math.max(4, findmax([2, 3])))
4. Math.max(1, Math.max(4, Math.max(2, findmax([3]))))
5. Math.max(1, Math.max(4, Math.max(2, 3))) // 4

在没有子程序的情况下,考虑<强>一个数组元素的最大值是元素本身。< /强>

您希望将问题分解,以便将输入数组分解为一个元素,然后开始反向工作并检查是否有较大的元素

一些让您开始的伪代码:

max (A[1, 2, ..., n])
    if n = 1
        return A[1]
    else
        oldMax <- max([2, ..., n])
        if A[1] > oldMax
            return A[1]
        else
            return oldMax
在最后阶段,我们可以返回4,因为这是最大值。但是现在我们又回到了数组是
[3,4]
的阶段。在这个阶段,我们将检查第一个元素
3
,是否大于我们从递归调用
4
计算的最大值。在本例中,它不是,因此我们继续返回
4


我们重复这个过程直到回到第一层,在那里我们最终返回
4
,因为
1
不大于目前为止的最大值,
4
。由于数组中没有一个元素大于<代码> 4 ,最大值从未改变。

< P>在没有子程序的情况下,考虑<强>一个数组元素的最大值是元素本身。< /强>

您希望将问题分解,以便将输入数组分解为一个元素,然后开始反向工作并检查是否有较大的元素

一些让您开始的伪代码:

max (A[1, 2, ..., n])
    if n = 1
        return A[1]
    else
        oldMax <- max([2, ..., n])
        if A[1] > oldMax
            return A[1]
        else
            return oldMax
在最后阶段,我们可以返回4,因为这是最大值。但是现在我们又回到了数组是
[3,4]
的阶段。在这个阶段,我们将检查第一个元素
3
,是否大于我们从递归调用
4
计算的最大值。在本例中,它不是,因此我们继续返回
4


我们重复这个过程直到回到第一层,在那里我们最终返回
4
,因为
1
不大于目前为止的最大值,
4
。由于数组中没有一个元素大于
4
,因此最大值从未改变。

您知道
Math.max()
会自己在数组中找到最大值吗<代码>变量最大值=数学最大值应用(数学,arr)您知道
Math.max()
会自己在数组中找到最大值吗<代码>变量最大值=数学最大值应用(数学,arr)编辑没有默认值的代码。谢谢仔细考虑一下……编辑一个没有默认值的。谢谢我要仔细考虑一下。。。。