0% found this document useful (0 votes)
25 views3 pages

Divide and Conquer Algorithm - Minimum

1. The document describes a divide and conquer algorithm for finding the minimum element in an array. 2. The algorithm recursively divides the array in half at each step. 3. An analysis shows that the recursion tree has a height of log n, so the time complexity is O(log n).

Uploaded by

Hakim Al-Huribi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
25 views3 pages

Divide and Conquer Algorithm - Minimum

1. The document describes a divide and conquer algorithm for finding the minimum element in an array. 2. The algorithm recursively divides the array in half at each step. 3. An analysis shows that the recursion tree has a height of log n, so the time complexity is O(log n).

Uploaded by

Hakim Al-Huribi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

KING KHALID UNIVERSITY

College of Computer Science

Student Name1: ‫دمحم ماجد دمحم أل سعود‬

Divide and Conquer Algorithm for Finding Minimum in an Array

Activity 1: Coding (1 point)

Program Code :
public class DivideAndConquerMinimum {
public static int findMinimum(int[] arr) {
return findMinimumUtil(arr, 0, arr.length - 1);
}

private static int findMinimumUtil(int[] arr, int start, int end) {


// Base case: single-element subarray
if (start == end) {
return arr[start];
}

int mid = (start + end) / 2;


// Minimum in the left subarray
int leftMin = findMinimumUtil(arr, start, mid);
// Minimum in the right subarray
int rightMin = findMinimumUtil(arr, mid + 1, end);
// Compare and return the overall minimum
return Math.min(leftMin, rightMin);
}

public static void main(String[] args) {


int[] arr = {5, 7, 3, 9, 2, 6};
int min = findMinimum(arr);
System.out.println("Minimum value: " + min);
}

}
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)

findMin(arr, 0, 1) findMin(arr, 2, 3) findMin(arr, 4, 5) findMin(arr, 6, 7)

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7]

from above the recursion tree has a height of log(n) as the problem size reduces by
half at every level.

You might also like