简体   繁体   中英

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). 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) -

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.

Why the recursive code has O(n) time?

It solves n-1 smaller problems. And n th problem is solved in O(1) time from the result of n-1 th problem.

How the recursive code works?

If you have an array with n sized array, then maximum element of this array is either
1. last element array[n-1] , or
2. the maximum element of the n-1 sized array.

To calculate the result of n-1 sized array, you repeat similarly.

The base case is, when you have only one element in the array, then that is the maximum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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