Question

You have to write your code in C++ (as a cpp file) and prepare a docx...

You have to write your code in C++ (as a cpp file) and prepare a docx file that formally explains the complexity of your implemented algorithm (using recurrence relations). For details, please read the questions carefully.

Question 1:

Use a divide and conquer approach based algorithm that finds the largest item in a given array of size n. Analyze your algorithm and show the complexity by solving its recurrence relation (cpp file for the code and a docx file for the complexity analysis).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE (cpp file)

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// Divide & Conquer solution to find minimum and maximum number in an array
void find_Max(int arr[], int low, int high, int& max)
{
// if array contains only one element
if (low == high) // common comparison
{
if (max < arr[low]) // comparison 1
max = arr[low];

return;
}

// if array contains only two elements
if (high - low == 1) // common comparison
{
if (arr[low] < arr[high]) // comparison 2
{
if (max < arr[high]) // comparison 3
max = arr[high];
}
else
{
if (max < arr[low]) // comparison 4
max = arr[low];
}
return;
}

// find mid element
int mid = (low + high) / 2;

// recur for left sub-array
find_Max(arr, low, mid, max);

// recur for right sub-array
find_Max(arr, mid + 1, high, max);
}

int main()
{
cout<<"Enter the number of elements\n";
int n;
cin>>n;
  
cout<<"\nEnter the elements\n";
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];

int max = INT_MIN;
find_Max(arr, 0, n - 1, max);

cout<<"\nThe maximum element in the array is " << max;
   cout<<endl;
return 0;
}

IMAGE OF OUTPUT

COMPLEXITY ANALYSIS (Docx file)

Using master theorem to solve the complexity of the algorithm. We have,

T(n) = 2T(n/2) +1 (recurrence formula for the algorithm used)

Here, a = 2 , b = 2 , k = 0 and c = 1

So, a > bk (condition 3 holds true) - as 2 > 1

Thus,
   => T(n) ~ nlog22   
   => T(n) ~ n

Time complexity of the algorithm is O(n).

Add a comment
Know the answer?
Add Answer to:
You have to write your code in C++ (as a cpp file) and prepare a docx...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conqu...

    (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT