0% found this document useful (0 votes)
14 views4 pages

Da Assignment 1

The document discusses implementing the min-max algorithm using a divide and conquer approach. It explains that the divide and conquer approach divides the array at the middle and recursively finds the min and max of the left and right halves, then combines the results to find the overall min and max in O(n) time instead of O(n^2) time for a brute force approach.

Uploaded by

samanthaargent21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Da Assignment 1

The document discusses implementing the min-max algorithm using a divide and conquer approach. It explains that the divide and conquer approach divides the array at the middle and recursively finds the min and max of the left and right halves, then combines the results to find the overall min and max in O(n) time instead of O(n^2) time for a brute force approach.

Uploaded by

samanthaargent21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DepartmentofInformation Technology

Experiment
1(DivideandConquer)
Aim: Implementationofmin-max algorithmusingdivideandconquer.
Theory:
Max-Min problem is to find a maximum and minimum element from the given array. We
caneffectivelysolveit usingdivideand conquerapproach.
In the traditional approach, the maximum and minimum element can be found by
comparingeachelementandupdatingMaxandMinvaluesasandwhenrequired.Thisapproachissim
plebutitdoes(n–1)comparisonsforfindingmaxandthesamenumberofcomparisonsforfindingthe
min. It results in a total of 2(n – 1) comparisons. Using a divide and conquer approach,
wecanreducethe number of comparisons.
Divideandconquer approachforMax.Minproblem worksin threestages.

• If a1 is theonlyelement in the array, a1 is themaximum and minimum.


• If the array contains only two elements a1 and a2, then the single comparison
betweentwoelements can decidethe minimumand maximum of them.
• If there are more than two elements, the algorithm divides the array from the
middleandcreatestwosubproblems.Bothsubproblemsaretreatedasanindependentproble
mandthe same
recursiveprocessisappliedtothem.Thisdivisioncontinuesuntilsubproblemsizebecomes
oneor two.
After solving two subproblems, their minimum and maximum numbers are compared to
buildthe solution of the large problem. This process continues in a bottom-up fashion to build
thesolutionof aparent problem.

Algorithm:
AlgorithmDC_MAXMIN(A,low,high)
// Description : Find minimum and maximum element from array using divide and
conquerapproach
//Input :ArrayAoflength n,and indiceslow =0and high=n-1
//Output: (min,max)variables holdingminimum andmaximum elementofarray

ifn ==1 then


return (A[1],
A[1])elseif n
==2then
if A[1] < A[2]
thenreturn (A[1],
A[2])else
DepartmentofInformation Technology
return (A[2],
A[1])else
mid← (low+high)/ 2
[LMin, LMax] = DC_MAXMIN (A, low,
mid)[RMin, RMax] = DC_MAXMIN (A, mid + 1,
high)if LMax>RMaxthen// Combinesolution
max ←
LMaxelse
max ←
RMaxend
if LMin<RMinthen // Combine
solutionmin←LMin
else
min ←
RMinend
return (min,
max)end

Complexity:
Timecomplexity:O(n)

Example:

LabAssignment:
1. Write a program to find the minimum and maximum of the following array using
divideandconquerapproach.50, 40, -5, -9, 45, 90, 65, 25, 75
#include <stdio.h>

void find_min_max(int arr[], int low, int high, int *min, int *max) {
if (low == high) { // Base case: Only one element in the array
*min = *max = arr[low];
DepartmentofInformation Technology
} else if (high - low == 1) { // Base case: Only two elements in the array
if (arr[low] < arr[high]) {
*min = arr[low];
*max = arr[high];
} else {
*min = arr[high];
*max = arr[low];
}
} else {
int mid = (low + high) / 2;
int min1, max1, min2, max2;

// Divide the array into two parts and recursively find min/max in each part
find_min_max(arr, low, mid, &min1, &max1);
find_min_max(arr, mid + 1, high, &min2, &max2);

// Combine the results


*min = (min1 < min2) ? min1 : min2;
*max = (max1 > max2) ? max1 : max2;
}
}

int main() {
int arr[] = {50, 40, -5, -9, 45, 90, 65, 25, 75};
int size = sizeof(arr) / sizeof(arr[0]);
int min_val, max_val;

find_min_max(arr, 0, size - 1, &min_val, &max_val);


printf("Minimum: %d\n", min_val);
printf("Maximum: %d\n", max_val);
DepartmentofInformation Technology
return 0;
}

You might also like