Use the divide-and-conquer approach to write an algorithm that finds the largest item
in a list of n items. Analyze your algorithm, and show the results in order notation
Asked
Active
Viewed 424 times
-3
-
1Why would you want to use a divide-and-conquer approach if can just go through the entire (unsorted, since you didn't specify otherwise!) list in a linear scan to find the largest item? – Mo B. Apr 20 '22 at 11:02
-
1If the array is sorted, you can simply get the largest value as it will be the last element. If the array is not sorted, you cannot use divide and conquer to find the largest element, instead you should find it linearly – Tanay Apr 20 '22 at 11:37
-
Please provide enough code so others can better understand or reproduce the problem. – Community Apr 20 '22 at 19:25
1 Answers
0
A divide-and-conquer algorithm for finding the maximum element in an array would split the array into two halves, solve each subproblem separately and return the maximum of the solutions to the two subproblems. The base case of the recursion would be when the subproblem has size 1, in which case the maximum is the element in the array. The recurrence for the running time is $T(n)=2T(n/2)+c$, which has solution $T(n)=\Theta(n)$ by the Master theorem. This is the same asymptotic running time as (but a constant factor larger than) linear search. Here's the pseudocode:
Function FindMax(A,p,r):
#input: an array A[p..r]. Returns maximum value
if p=r then return A[p] #base case of recursion
else:
q = (p+r)//2 #midpoint
return max{FindMax(A,p,q), FindMax(A,q+1,r)}

Ashwin Ganesan
- 331
- 1
- 4