Question

3. (10 marks) Describe a recursive algorithm for finding the maximum element in a array A...

3. Describe a recursive algorithm for finding the maximum element in a array A of n elements. Analyze its time complexity using a recursion tree.

Homework Answers

Answer #1

Code:

#include <stdio.h>
int max(int a,int b)
{
    if(a>b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int find_max(int *a,int n)
{
    int i;
    if(n==1)
    {
        return a[0];
    }
    return max(a[n-1],find_max(a,n-1));
}
int main()
{
    int a[10]={9,10,3,20,5,8,2,1,19,16};
    int b=find_max(a,sizeof(a)/sizeof(a[0]));
    printf("Maximum=%d",b);
    return 0;
}

Screenshots:

Time complexity

T(n) = 1+ T(n-1)

Time complexity = 1+1+1+...... n times = O(n)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java 1. Write a recursive algorithm to add all the elements of an array of...
In 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...
Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the...
Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the maximum elements in an array A of n elements. Your algorithm calls itself only once within the algorithm and should return a pair (a, b) where a is the minimum element and b is the maximum element. Algorithm Min-Max(A, n) Input: an Array A of n elements Output: a pair of (a, b) where a is the minimum element and b is the maximum...
Assume that we have a sorted array a[n]with n non-negative numbers. a. Develop an algorithm using...
Assume that we have a sorted array a[n]with n non-negative numbers. a. Develop an algorithm using divide-and-conquer to search for an element x in this sorted array a[n]. This algorithm will take an input of a sorted array a[n], and return the index of element x is an element of a[n], or return -1if xis NOT an element of this array. b. Analyze the time complexity of this algorithm.
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[],...
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[], n) { // Base case if (n == 1) return;    // One pass of bubble sort. After // this pass, the largest element // is moved (or bubbled) to end. for (i=0; i<n-1; i++) if (A[i] > A[i+1]) swap(A[i], A[i+1]);    // Largest element is fixed, // recur for remaining array bubbleSort(A, n-1); }
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Describe an algorithm that selects and sorts k smallest elements from an array A of length...
Describe an algorithm that selects and sorts k smallest elements from an array A of length n of integers. Estimate the running time. The efficiency of your algorithm is important.
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
A is an array with n elements such that each element is at most in k...
A is an array with n elements such that each element is at most in k position away of its ordered position. Give an algorithm that sorts the elements of A on O(n log k). Example: [2,1,4,3]
Question Recursive Maximum: In this question you must develop and use a recurive function to find...
Question Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. Note that the function max is not recursive....
a) Design a recursive linear-time algorithm that tests whether a binary tree is a binary search...
a) Design a recursive linear-time algorithm that tests whether a binary tree is a binary search tree. Describe your algorithm in English or with a simple pseudocode program. b) (3 bonus pts.) Extend the algorithm in a) to test whether a binary tree is an AVL tree.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT