Question

Determine the basic operation(s) in the following algorithm and then analyze its performance for i =...

Determine the basic operation(s) in the following algorithm and then analyze its performance

     for i = 1 to n do
     {

}

for j = i to n do
     x++;
for j = 1 to i do
     y++;
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Number of times each line runs in the code 
for i = 1 to n do   -> (n+1)
{                   -> 1
}                   -> 1

for j = i to n do   -> (n+1)
     x++;           -> (n)
for j = 1 to i do   -> n(n+1)
     y++;           -> n(n)


Total time complexity 
= (n+1)+1+1+(n+1)+n+n(n+1)+n(n)
= 2(n+1)+2+n+n(n+1+n)
= 2(n+1)+2+n+n(2n+1)
= 2n+2+2+n+2n^2+n
= 4n+4+2n^2
= O(n^2)
Add a comment
Know the answer?
Add Answer to:
Determine the basic operation(s) in the following algorithm and then analyze its performance for i =...
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
  • Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for...

    Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for i = 1 to 5n^2 do p = p times i return p (b) Algorithm for-loop(n): s = 0 for i = 1 to n do for j = I to n do s = s + i return s (c) Algorithm WhileLoop(n): x = 0; j = 2; while (j = n){x = x+ 1; j =j times 2;}

  • Algorithm performance Give the order of magnitude Theta () for the following algorithm. Explain why your...

    Algorithm performance Give the order of magnitude Theta () for the following algorithm. Explain why your answer is correct. GET VALUES for A_1, A_2, .. ., A_n, and B_1, B_2, .. ., B_n Get value of n i = 1/* set i equal to 1 */DO WHILE (i lessthanorequalto n)/* for each of the n values in A */j = 1/* set j equal to 1 */DO WHILE (j lessthanorequalto n)/* Do n times *1 IF Ai = Bj THEN...

  • 1. Implement the merge-sort algorithm a. Determine the correct basic operation 2. Test your algorithm on...

    1. Implement the merge-sort algorithm a. Determine the correct basic operation 2. Test your algorithm on inputs of size 2^10, 2^11 ... 2^20. If this is too slow then just run it on any set of powers of two. 3. Show the running time of each of these inputs Choose a basic operation Count the total number times that this basic operation is done for each power of 2. Display this count in a table

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • ALGORITHM X(A[0..n - 1]) // Input: A contains n real numbers for it 0 to n...

    ALGORITHM X(A[0..n - 1]) // Input: A contains n real numbers for it 0 to n - 2 do for jt i +1 to n - 1 do if Aj] > A[i] swap A[i] and A[j] 1. What does this algorithm compute? 2. What is the basic operation? 3. How many times is the basic operation executed? 4. What is the efficiency class of this algorithm?

  • Perform the following to the algorithm below: - - Express T(n) as a function of n...

    Perform the following to the algorithm below: - - Express T(n) as a function of n Find a best approximation for the Big O function for T(n) Perform a time complexity analysis Define the basic operation of the algorithm Correctness Efficiency - - Procedure maxMin (n, A, I, h) integer h, I, A (1:n), n integer j j-2 IA (1) hS (1) while (i <=n) do if (Ali) < 1) then TEA (0) if(Ali) >h) then h A() j+į+1 repeat...

  • 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; }...

  • Analyze the time complexity of the following algorithm. You may assume that the floor function in...

    Analyze the time complexity of the following algorithm. You may assume that the floor function in line 2 takes Theta (1) time. Please show your work. Input: data: array of integers Input: n: size of data Output: median of data 1 Algorithm: MedianSelect 2 lim = [n/2] + 1 3 min = - infinity 4 for i = 1 to lim do 5 prev = min 6 min = infinity 7 for j = 1 to n do 8 if...

  • 1. Analyze the running time of the following algorithm and write it using ( notation. You...

    1. Analyze the running time of the following algorithm and write it using ( notation. You must show detailed calculation/derivation of your running time along with table to get marks. int sum = 0; for (int i=n; i)=1; i=i/2) { for (int j=1; j<=i; j*=2) { sum+=i*j; } }

  • By concentrating on the dominating step(s) and also assuming that all basic operations are having...

    Please explain your thought process as your complete this problem By concentrating on the dominating step(s) and also assuming that all basic operations are having the same constant cost C, compute Tworst-case(n) in closed-form for the following program segment. Do not simplify your solution. Remark: You must first set up the summation equation for Tworst-case(n) and then evaluate the sums. x = 168; y-268; for i-1 to n do for J-to n do y=x-y; for k-1 to i do y...

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