简体   繁体   English

用于查找数组中最大数的递归算法的运行时间复杂度是多少。 比较它与迭代循环代码

[英]What will be the run time complexity for recursive algorithm for finding the largest number in an array. Compare it vs iterative loop code

I have written the code for the largest number in the iteration loop code.我已经编写了迭代循环代码中最大数字的代码。 And I have found the run time complexity for the code is O(n).而且我发现代码的运行时间复杂度是 O(n)。 What will be the run time complexity for the recursive code of the largest number and how will it differ from the iteration loop.最大数量的递归代码的运行时间复杂度是多少,它与迭代循环有何不同。 Which will be better.哪个会更好。 My code for the iteration loop is我的迭代循环代码是

package com.bharat;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the numbers you want to add in the array: ");
        int number = scanner.nextInt();
        int[] myIntgersArray = getIntegers(number);

        System.out.println(Arrays.toString(myIntgersArray));
        System.out.println(findBiggestNumber(myIntgersArray));
    }


    public static int[] getIntegers(int number){
        Scanner scanner = new Scanner(System.in);
        int[] values= new int[number];
        for (int i =0;i<values.length;i++){
            values[i]=scanner.nextInt();
        }
        return values;
    }

    public static int  findBiggestNumber(int[] array){
        int i=0;
        int biggestNumber = array[i];
        for ( i = 0;i<array.length;i++){
            if (array[i]>biggestNumber){
                biggestNumber=array[i];
            }
        }
        return biggestNumber;
    }
}

Recursive code which was posted in comments -在评论中发布的递归代码 -

public static int findBiggestNumber(int[] array, int number) { 
    int highestNumber = array[0]; 

    if (number==1) { 
        return highestNumber; 
    } 
    else { 
        if (highestNumber < array[number]) {
            array[number] = highestNumber; 
            return highestNumber; 
        } 
    }

    return findBiggestNumber(array, number-1); 
} 

Correct recursive code (assuming the array has at least 1 element) -正确的递归代码(假设数组至少有 1 个元素) -

public static int findBiggestNumber(int[] array, int number)
{ 

    if(number == 1)             //only one element is there in array
    {
        return array[number -  1];
    }


    return Math.max( array[number - 1] , findBiggestNumber(array,number-1) ); 
} 

The recursive code runs in O(n) time but it has extra space overhead of O(n) due to the recursive function call stack.递归代码在 O(n) 时间内运行,但由于递归 function 调用堆栈,它具有 O(n) 的额外空间开销。

Why the recursive code has O(n) time?

It solves n-1 smaller problems.它解决n-1较小的问题。 And n th problem is solved in O(1) time from the result of n-1 th problem.从第n-1个问题的结果中,第n个问题在 O(1) 时间内得到解决。

How the recursive code works?

If you have an array with n sized array, then maximum element of this array is either如果您有一个具有n大小数组的数组,则该数组的最大元素是
1. last element array[n-1] , or 1. 最后一个元素array[n-1] ,或
2. the maximum element of the n-1 sized array. 2. n-1大小数组的最大元素。

To calculate the result of n-1 sized array, you repeat similarly.要计算n-1大小数组的结果,请类似地重复。

The base case is, when you have only one element in the array, then that is the maximum.基本情况是,当数组中只有一个元素时,这是最大值。

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

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