Divide and Conquer Algorithm - Minimum
Divide and Conquer Algorithm - Minimum
Program Code :
public class DivideAndConquerMinimum {
public static int findMinimum(int[] arr) {
return findMinimumUtil(arr, 0, arr.length - 1);
}
}
Screenshot of Running the code
Activity 2: Complexity Analysis (1 point)
After implementing the code, analyze the time complexity of your algorithm.
Time Complexity Analysis:
The time complexity of this algorithm can be analyzed using Proof by Recursion
Tree.
Each recursive call divides the array into two halves, resulting in a binary tree. At
each level of the tree, the number of subproblems is halved. The height of the tree
is equal to the logarithm base 2 of the input size. Therefore, the time complexity of
this algorithm is O(log n), where n is the size of the input array.
The recursive function findMin() takes the array and ranges low and high as
parameters.
It divides the problem into sub-problems by dividing the array into halves:
- Left subarray: arr[low..mid]
- Right subarray: arr[mid+1..high]
Base case: When low == high, the subarray has a single element, we return that
element.
Below is the recursion tree for an array of size n=8:
log(n)
findMin(arr, 0, 7)
findMin(arr, 0, 3) findMin(arr, 4, 7)
from above the recursion tree has a height of log(n) as the problem size reduces by
half at every level.