Question

In: Computer Science

write a recursive algorithm to find the maximum element in an array of n elements and...

write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency.

(I am using c++ programming language)

Solutions

Expert Solution

#include <iostream>
using namespace std;

// Function to find maximum element in an array of n elements
// i is the current index
// size is the length of array
// arr is the array name
// max is the function name
int max(int arr[], int i, int size){
   // if the i+1 == size is true that means i is the last index of the array
   if(i+1 == size){
      // Then returning element at index i
      return arr[i];
   }
   else{
      // Calculating max for the remaining array starts from index i+1
      int m = max(arr, i+1, size);
      
      // If the max of remaining array is larger than arr[i]
      // Then returning m as the max of the array 
      if(m > arr[i]){
         return m;
      }
      // If the max of remaining array is not larger than arr[i]
      // Then returning arr[i] as the max of the array
      else{
         return arr[i];
      }
   }
}

int main() {
   //Testing
   int arr[] = {4,2,5,1,3,-1};
   int i = 0, size = 6;
   // Making function call to max and printing the returned value to console
   cout<<"Max = "<<max(arr, i, size)<<endl;
   return 0;
}

Base case:
T(1) = 1
Recursive case:
T(n) = T(n-1) + 1
Solving recursive formula:
T(n) = T(n-1) + 1
     = T(n-2) + 1 + 1
     = T(n-3) + 1 + 1 + 1
     ....
     ....
     = T(n-(n-1)) + 1 + ... + 1 + 1 + 1
     = T(1) + 1 + ... + 1 + 1 + 1
     = 1 + 1 + ... + 1 + 1 + 1
     = n
     = O(n)

Related Solutions

Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT