Question

Consider the following recursive method for finding the maximum element in an int array: public int...

Consider the following recursive method for finding the maximum element in an int array:

public int static max(int[] a, int lo, int hi) {
  if (lo > hi) return a[lo];
  int mid = (lo + hi) / 2;
  int loMax = max(a, lo, mid);
  int hiMax = max(a, mid+1, hi);
  if (loMax > hiMax)
    return loMax;
  else
    return hiMax;
}

Write down the recurrence relation for counting the number of times the comparison if (loMax > hiMax) is performed. Use the recurrence relation for merge sort as a model.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Add a comment
Know the answer?
Add Answer to:
Consider the following recursive method for finding the maximum element in an int array: public int...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting...

    2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.

  • plz solve Modify binary search so that it always returns the element with the smallest index...

    plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...

  • 4. (5 Points) The following is another merge sort top down implementation, what is the running...

    4. (5 Points) The following is another merge sort top down implementation, what is the running time and space complexity for this implementation in big-0? Briefly explain your answer. public static 〈T extends Comparable〈 T> > void sort2(T[] a) { sort2(a, , a.length - 1); @Suppresswarnings ("unchecked") private static <T extends Comparable<T>> void sort2(ΤΠ a, intlo, int hi) { if (hi (z lo) return; T[] aux- (TLD new comparable[a,length]; int mid- (10 + hi) / 2; sort2 (a, lo, mid);...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Strange Sort In a dumpster outside MEB, I found a scrap of paper with this Java...

    Strange Sort In a dumpster outside MEB, I found a scrap of paper with this Java implementation of a sorting algorithm on it: // This will get me the Turing Award for sure!! public static void sort (int[] A) { sort (A, O, A.length); // Sorts the subarray Allo .. hi-1] into ascending order private static void sort (int[] a, int lo, int hi) { int size = hi - lo; if (size == 2) { if (A[lo] > A[10+1])...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by re...

    PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by refernce, Structure) a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for a key point in the rotated array in O(logn) time, where the rotation Rotates the sorted array around a random point, e.g., 1 2 345->34512 is point is known in advance. ts above by first finding the unknown rotation point...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT