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...
You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide java code...
You are a given an array of n elements, design an algorithm to find the minimum...
You are a given an array of n elements, design an algorithm to find the minimum element and the 2nd minimum element in the array using as few comparisons as possible. For example, A[] can be [5, 6, 7, 3, 2, 1]. You should output 1 as minimum, 2 as 2nd minimum element in the array, and at the same time, minimize the number of comparisons used. (i) describe the idea behind your algorithm in English (ii) provide pseudocode (iii)...
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.
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
Given a stack S and an element x, write a recursive algorithm that returns True if...
Given a stack S and an element x, write a recursive algorithm that returns True if x is in S, or False otherwise. Note that your algorithm should not change the content in S. What is the time complexity of your algorithm?
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...
Think of an algorithm to find the maximum value in an (unsorted) array. Now, think of...
Think of an algorithm to find the maximum value in an (unsorted) array. Now, think of an algorithm to find the second largest value in the array. Which is harder to implement? Which takes more time to run (as measured by the number of comparisons performed)? Now, think of an algorithm to find the third largest value. Finally, think of an algorithm to find the middle value. Which is the most difficult of these problems to solve?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT