0% found this document useful (0 votes)
6 views

Algorithm Assignment

Algorithms assignment

Uploaded by

Uzair Muhktar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Algorithm Assignment

Algorithms assignment

Uploaded by

Uzair Muhktar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Algorithm Design Assignment

1. Design of the Algorithm

The problem is to find the maximum and minimum elements in an array using the Divide and

Conquer approach, minimizing the number of comparisons. The divide-and-conquer algorithm works

as follows:

Divide: Split the array into two halves.

Conquer: Recursively find the maximum and minimum in each half.

Combine: Compare the two maxima from the halves to get the overall maximum, and compare the

two minima to get the overall minimum.

Steps of the Algorithm:

1. Base Case: If the array contains only one element, that element is both the maximum and

minimum.

2. Recursive Case: Split the array into two halves and recursively find the maximum and minimum

for each half.

3. Combine: The overall maximum will be the larger of the two maxima, and the overall minimum will

be the smaller of the two minima from both halves.

2. Implementation in C++

#include <iostream>

#include <algorithm>

using namespace std;


pair<int, int> findMaxMin(int arr[], int low, int high) {

if (low == high) {

return {arr[low], arr[low]};

if (high == low + 1) {

if (arr[low] > arr[high]) {

return {arr[low], arr[high]};

} else {

return {arr[high], arr[low]};

int mid = (low + high) / 2;

pair<int, int> left = findMaxMin(arr, low, mid);

pair<int, int> right = findMaxMin(arr, mid + 1, high);

int finalMax = max(left.first, right.first);

int finalMin = min(left.second, right.second);

return {finalMax, finalMin};

int main() {

int arr[] = {3, 5, 1, 8, 9, 2, 6, 4};

int n = sizeof(arr) / sizeof(arr[0]);

pair<int, int> result = findMaxMin(arr, 0, n - 1);

cout << "Maximum element: " << result.first << endl;

cout << "Minimum element: " << result.second << endl;

return 0;

}
3. Time Complexity Analysis

The brute-force method for finding both maximum and minimum involves scanning the array and

comparing each element with both the current maximum and minimum, leading to a total of 2n - 2

comparisons for an array of size n.

Divide and Conquer Method:

The algorithm divides the array into two halves, recursively solves each half, and combines the

results.

For larger arrays, the recursion ensures fewer comparisons:

The total number of comparisons for an array of size n using this approach is 3n/2 - 2.

This reduction in the number of comparisons is significant for large arrays, making the

divide-and-conquer approach more efficient.

Comparison to Brute-Force Approach:

Brute-force requires 2n - 2 comparisons. Divide and Conquer performs only about 3n/2 - 2

comparisons, which is fewer.

4. Deliverables

A concise report explaining the algorithm design, time complexity analysis, and the advantages of

using divide-and-conquer over brute-force.

Source code in C++ along with sample inputs and outputs:

Sample Input: {3, 5, 1, 8, 9, 2, 6, 4}

Sample Output: Maximum element: 9, Minimum element: 1

You might also like