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

Daa 1 (Mergesort)

The document outlines an assignment to write a C program that finds the maximum and minimum elements in an array using a divide and conquer approach. It provides a detailed algorithm and corresponding code implementation, including base cases and recursive cases for the function. The complexity analysis indicates a time complexity of O(n) and a space complexity of O(log n).

Uploaded by

rohxn16
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)
3 views3 pages

Daa 1 (Mergesort)

The document outlines an assignment to write a C program that finds the maximum and minimum elements in an array using a divide and conquer approach. It provides a detailed algorithm and corresponding code implementation, including base cases and recursive cases for the function. The complexity analysis indicates a time complexity of O(n) and a space complexity of O(log n).

Uploaded by

rohxn16
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/ 3

ASSIGNMENT 1

PROBLEM STATEMENT
Write a program in C to find out maximum and minimum elements in an array using divide
and conquer approach.

ALGORITHM
1. Input: An array `ARR` of size `N`, and two indices `LOW` and ` HIGH ` representing the
current segment of the array.
2. Base Cases:
- IF `LOW == HIGH`:
- This means there is only one element in the current segment.
- Set `max = ARR [LOW]` and `min = ARR [LOW]`.
- Return the result.
- If ` HIGH == LOW + 1`:
- This means there are two elements in the current segment.
- Compare ` ARR [LOW]` and ` ARR [HIGH]`:
- If ` ARR [LOW] > ARR [HIGH]`, set `max = ARR [LOW]` and `min = ARR [HIGH]`.
- Otherwise, set `max = ARR [HIGH]` and `min = ARR [LOW]`.
- Return the result.
3. Recursive Case:
- Calculate the midpoint: `mid = (LOW + HIGH) / 2`.
- Recursively find the maximum and minimum in the left half of the array:
- Call `findMinMax(ARR, LOW, mid)` and store the result in `leftResult`.
- Recursively find the maximum and minimum in the right half of the array:
- Call `findMinMax(ARR, mid + 1, HIGH)` and store the result in `rightResult`.
4. Combine Results:
- Set `result.max` to the maximum of `leftResult.max` and `rightResult.max`.
- Set `result.min` to the minimum of `leftResult.min` and `rightResult.min`.
5. Output: Return the combined result containing the maximum and minimum values.
CODE
#include <stdio.h>
#include <limits.h>

typedef struct {
int max;
int min;
} MinMax;

MinMax findMinMax(int arr[], int low, int high) {


MinMax result;
if (low == high) {
result.max = arr[low];
result.min = arr[low];
return result;
}
if (high == low + 1) {
if (arr[low] > arr[high]) {
result.max = arr[low];
result.min = arr[high];
} else {
result.max = arr[high];
result.min = arr[low];
}
return result;
}
int mid = (low + high) / 2;
MinMax leftResult = findMinMax(arr, low, mid);
MinMax rightResult = findMinMax(arr, mid + 1, high);
result.max = (leftResult.max > rightResult.max) ? leftResult.max : rightResult.max;
result.min = (leftResult.min < rightResult.min) ? leftResult.min : rightResult.min;

return result;
}

int main() {
int arr[] = {20, 2, 40, 4, 50};
int n = sizeof(arr) / sizeof(arr[0]);

MinMax result = findMinMax(arr, 0, n - 1);

printf("Maximum element: %d\n", result.max);


printf("Minimum element: %d\n", result.min);

return 0;
}

OUTPUT

COMPLEXITY ANALYSIS
- Time Complexity: The time complexity of this algorithm is (O(n)) because each element is
compared a constant number of times.
- Space Complexity: The space complexity is (O(log n)) due to the recursive call stack in the
worst case.

You might also like