Question

Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...

Given the following algorithm, matching each statement to the correct sequence for complexity analysis.

procedure Alg3(A): A is a list of n integers

1 for i = 1 to n-1 do

2   x=aix=ai

3 j = i − 1

4 while (j ≥≥ 0) do

5 if x≥ajx≥aj then

6 break

7 end if

8   aj+1=ajaj+1=aj

9 j = j − 1

a end while

b   aj+1=xaj+1=x

c end for

d return A

The complexity of this algorithm is O(n2)O(n2)

Respuesta 1Elegir...1572634

The inner loop is a while loop for variable jj and the number of iterations is i−1i−1 in the worst case.

Respuesta 2Elegir...1572634

The outer loop is a for loop for variable ii and the number of iterations is n−1n−1.

Respuesta 3Elegir...1572634

In each iteration in the inner loop, there are 2 operations.

Respuesta 4Elegir...1572634

We consider comparison as the operation to be analyzed

Respuesta 5Elegir...1572634

There are at most 2×(1+2+⋯+(n−1)+2(n−1)+12×(1+2+⋯+(n−1)+2(n−1)+1 operation.

Respuesta 6Elegir...1572634

The pseudocode contains a nested loop.

Respuesta 7Elegir...1572634

Homework Answers

Answer #1

If you like th solution please give it a thumbs up.

Solution :- Clearly, text in the question in gambled, but i can understand the steps. below i've ordered the time complexity analysis according to the given code.

- The pseudocode contains a nested loop.

- The outer loop is a for loop for variable i and the number of iterations is n−1.

- The inner loop is a while loop for variable j and the number of iterations is i−1 in the worst case.

- In each iteration in the inner loop, there are 2 operations.

- We consider comparison as the operation to be analyzed

-  There are at most 2×(1+2+⋯+(n−1)+2(n−1)+1 operation.

- The complexity of this algorithm is O(n2).

Know the answer?
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
Similar Questions
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1:...
1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1: foo mystery() foreach i ←1 to n do mystery() foreach j ←1 to i do (A) Determine the exact number of times mystery() is called in terms of n. (B) Assume the mystery function is in O(log(n) ∗ n ^2 ). Determine the complexity of “foo” in O-notation. if i ≤ j then mystery()
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[],...
Given the recursive bubble sort algorithm, analyze its time complexity in terms of Big O. bubbleSort(A[], n) { // Base case if (n == 1) return;    // One pass of bubble sort. After // this pass, the largest element // is moved (or bubbled) to end. for (i=0; i<n-1; i++) if (A[i] > A[i+1]) swap(A[i], A[i+1]);    // Largest element is fixed, // recur for remaining array bubbleSort(A, n-1); }
2. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n...
2. Design a deterministic algorithm to solve the following problem. input: An array A[1...n] of n integers. output: Two different indices i and j such that A[i] = A[j], if such indices exist. Otherwise, return NONE. Your algorithm must take O(n log(n)) time. You must describe your algorithm in plain English (no pseudocode) and you must explain why the running time of your algorithm is O(n log(n)).
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 = 0; sum5 = 0; for(i=1; i<=n/2; i++) { sum2 = sum + 2; } for(j=1; j<=n*n; j++) { sum5 = sum + 5; } i think it is O(n^2) since big oh finds the order of worst case which is the the second loop being n^2 complexity. but im not sure. 2.) Give an efficient algorithm along with running time analysis to find the...
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;...
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;    l=0; u= n-1;    while (l<u) {        m= (l+u)/2;        if (a[m] < x) l= m+1;        else if (a[m] >x) u=m-1;            else return “found”    }    return (“not found”); }
Find the exact number of times (in terms of n) the innermost statement (X = X...
Find the exact number of times (in terms of n) the innermost statement (X = X + 1) is executed in the following code. That is, find the final value of X. Then express the total running time in terms of O( ), Ω( ), or Θ( ) as appropriate. X = 0; for k = 1 to n     for j = 1 to n – k         X = X + 1; The following program computes and returns...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
An online/anytime sorting algorithm is one that reads its input numbers one at a time, and...
An online/anytime sorting algorithm is one that reads its input numbers one at a time, and maintains a sorted array of all the inputs it has seen so far, so that if it is interrupted at any time, it will still output the correct answer for the inputs that it has processed. Not all sorting algorithms are amenable to modification to make them anytime. But one sorting algorithm, Bubble Sort, is easy to so modify. First, understand the ascending order...