Question

In: Computer Science

Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array...

Answer the following questions(5pt)
a)   Write an algorithm that finds the largest item in an array of n items by using divide-and-conquer algorithm design pattern.
b)   Analyze the worst-case time complexity of your algorithm in (a), and show the results using asymptotic notation (ϴ)

Solutions

Expert Solution

package search;

// Part (A)

//finds the largest item in an array of n items by using divide-and-conquer

public class DivideAndConqureSearch {

public static int mergeSearch(int ar[],int l,int r){

if(l<r){

int mid=(l+r)/2; //take mid index

int f=mergeSearch(ar,l,mid); //call mergesort on first half and return max in first half subaaray

int s=mergeSearch(ar,mid+1,r); //call mergesort on second half and return max in second half subaaray

int res=marge(ar,l,r,mid); //find the max in both side

return Integer.max(f, Integer.max(res, s)); //now return max of all three

}

return -1;

}

public static int marge(int ar[],int l,int r,int mid){

int res=Integer.MIN_VALUE; //Initialize res with min integer value

for(int i=l;i<=mid;i++){ //find the max in first half

if(ar[i]>res){

res=ar[i];

}

}

int res1=Integer.MIN_VALUE; //Initialize res1 with min integer value

for(int i=mid+1;i<=r;i++){ //find the max in second half

if(ar[i]>res){

res1=ar[i];

}

}

return Integer.max(res, res1); //return max of both

}

//Driver program to test

public static void main(String[] args) {

int ar[]={20, 13, 4, 34, 5, 15, 90, 100, 75, 102};

System.out.print(mergeSearch(ar,0,ar.length-1));

}

}

// Part (B)

/*worst case of merge sort is NlogN

* Merge sort

* {20, 13, 4, 34, 5, 15, 90, 100, 75, 102}

* / \

* {20, 13, 4, 34, 5} {15, 90, 100, 75, 102}

* / \ / \

* {20, 13, 4}{34, 5} {15, 90, 100,} {75, 102}

* / \ / \ / \ / \

*{20, 13} {4} {34} {5} {15, 90} {100} {75} {102}

* / \ / \

*{20} {13} {15} {90}

*

*Height of above tree is Log(N)

*to merge the array two temp array it will take o(N) in worst case

*so Worst Case time complexity it O(Nlog(N))

*

*

*/

output

102


Related Solutions

Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Consider the following algorithm to find the kth largest elementof a given array A of...
Consider the following algorithm to find the kth largest element of a given array A of n numbers. We pick every fifth element of A and place them in the array B. We find the median of B recursively, and use this median of B as a pivot to partition the array A. Depending on k and the number of elements that are smaller than the chosen pivot, we recurse into an appropriate subproblem of A.Answer the following questions.Write the...
Using the data in the following array write a program that finds and outputs the highest...
Using the data in the following array write a program that finds and outputs the highest value, the lowest value, the count of elements in the array, the average value in the array, and the mode - the value that appears most often. dataArray = {173.4, 873.7, 783.9. 000.0, -383.5, 229.5, -345.5, 645.5, 873.7, 591.2};
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
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...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If an element of the array is having more than one occurrence, then keep the first occurrence and remove all other occurrences of the element.
Write code that finds the largest number in data and stores it in the variable max...
Write code that finds the largest number in data and stores it in the variable max and the smallest number in data and stores it in min. Test Cases Test case #1 Expected result: max is 52.66; min is 15.53 Test case #2 Expected result: max is 56.95; min is 5.77 Test case #3 Expected result: max is 77.02; min is 24.24 Test case #4 Expected result: max is 90.48; min is 35.94.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write an algorithm which has a pre-populated array an array_input of array type and separates it,...
Write an algorithm which has a pre-populated array an array_input of array type and separates it, every other character, into two separate arrays called array1 and array2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT