Question

Below is an algorithm using C++ language Question : Analyze the algorithm and provide in terms...

Below is an algorithm using C++ language

Question : Analyze the algorithm and provide in terms of exact number of steps A(n) or the algorithm.
A(n) =

Given your analysis provide an upper bound in terms of Big-O for Algorithm .
O(n) =

#include <iostream>

#include <cstdlib>
#include <cmath>
using namespace std;

int random_value(int range, long seed) {
   srand(seed);
   float generator = (float) rand() / (float) RAND_MAX;
   float random = floor(range * generator);
   return random;
}

int main(){
   int n = 16;
   int m = 512;
   long seed = 36028797153181696;
   int initial[n];
   int sorted[n];

   for(int i = 0; i < n; i++) {
       initial[i] = random_value(m, seed/(i+1)) + 1;
   }
   for(int i = 0; i < n; i++){
       sorted[i] = 0;
   }
  
   cout << "L: (";
   for(int i = 0; i < n; i++){
       if(i > 0){
           cout << ", ";
       }
       cout << initial[i];
   }
   cout << ")\n";
   for(int i = 0; i < n; i++) {
       int v = m + 1;
       int s = 0;
      
       for(int j = 0; j < n; j++) {
           if(initial[j] > 0 && initial[j] < v) {
               v = initial[j];
               s = j;
           }
       }
       sorted[i] = initial[s];
       initial[s] = 0;
   }
   cout << "S: (";
   for(int i = 0; i < n; i++){
       if(i > 0){
           cout << ", ";
       }
       cout << sorted[i];
   }
   cout << ")\n\n";
   return 0;
}

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

Solution:

Explanation:

We can see in main() function ,

=>for(int i =0;i<n;i++) will run n times from i=0 to i=n-1 independenty.

=>After that there is a loop for(int i =0 ;i<n;i++) which will run n times from i = 0 to i = n-1 indpendently.

=>After that there is a loop for(int i =0 ;i<n;i++) which will run n times from i = 0 to i = n-1 indpendently.

=>After loop there is a loop for(int i =0 ;i<n;i++) which will run n times from i = 0 to i = n-1 indpendently.

=> After that there is a loop for(int j =0 ;j<n;j++) from j = 0 to j = n-1 which will run n times for every value of i=0 to i=n-1

=>After loop there is a loop for(int i =0 ;i<n;i++) which will run n times from i = 0 to i = n-1 indpendently.

From the above analysis we can calculate the number of times above loops will be executed :

=> n + n + n + n*n + n => 4*n + n*n

So A(n) = 4n + n2 (Exact number of steps)

Calculating upper bound:

Explanation:

=> From the above observations we found that 4n + n2 number of times loop will be executed.

=>Converting into upper bound:4n + n2 = O(n2)          (Asymototically)

So the upper bound of the algorithm is O(n2)

I have explained each and every part of the algorithm along with exact number of steps as well as an upper bound.

Add a comment
Know the answer?
Add Answer to:
Below is an algorithm using C++ language Question : Analyze the algorithm and provide in terms...
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
  • C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with...

    C++ ONLY!!! NOT JAVA. The standard deviation is calculated from an array X size n with the equation std=sqrt(sum((xi-mean)2/(n-1))) Sum the square of difference of each value with the mean. Divide that sum by n-1. Take the square root and you have the standard deviation. //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Srand #include <ctime> //Time to set random number seed #include <cmath> //Math Library #include <iomanip> //Format Library using namespace std; //User Libraries //Global Constants, no Global Variables...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

    Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() {    int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false;    srand(seed); // call the getRandom function below       tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...

    C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try...

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • In this lab we are going to complete a profile of two sorting algorithms by running...

    In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...

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