Question

In: Computer Science

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;

}

  1. Write a recursive version of this program.

  1. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n).

  1. Prove by induction that the solution of the recurrence is f(n) = n − 1.

Solutions

Expert Solution

Recursive version of this program:

Approach:

  1. We have to Recursively traverse the array from the end,
  2. Base case: If the remaining array is of length 1, then we have to return the only present element i.e. arr[0]
    if(n == 1)
       return arr[0];
  3. Recursive call: If the base case is not satisfied, then we will call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
  4. Return statement: At each recursive call (except for the base case), we have to return the maximum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call.
    return max(arr[n-1], recursive_function(arr, n-1));
    

Pseudocode:

Int MAX (int A [ ], int n) {
  if (n == 1) 
      return A[0]
  
  if(A[n-1] > MAX(A, n-1))
      return A[n-1]
  else
      return MAX(A, n-1)
}

Now, let f(n) be the number of key comparisons performed by this algorithm, then from the above recursive algorithm, f(n) = f(n - 1) + 1

Here, 1 denotes 1 key comparisons happening in base case.

Now, solving f(n), given f(n) = 1 (base case)

f(n) = f(n-1) + 1

f(n-1) = f(n-2) + 1

...

...

f(2) = f(1) + 1

Here, adding RHS and LHS, f(n) = f(1) + n - 2 = n - 1

Therefore f(n) = n - 1

Now, proving this using Induction:

Base Case: f(n) = 1

Assume for n = k, this function is satisfied: f(k) = k - 1

Now, Proving it for n = k + 1

f(k + 1) = k + 1 - 1 = k

So, true for n = k + 1

Hence, the expression is true,                                             PROVED


Related Solutions

// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++) { System.out.println(n); } System.out.println(); // 2. System.out.println("2."); int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); // 3. System.out.println("3."); for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1;...
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
Element Shifter: Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The...
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)
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
This code assigns the maximum of the values 3 and 5 to the int variable max...
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result int max; // your code goes here This code prompts the user for a single character and prints "true" if the character is a letter and "false" if it is not a letter // your code goes here
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...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT