Question

(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n:

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

Answer:

The folowing table represent each value after line 10 of the code is executed:-

1)

i j x a1 a2 a3 a4
2 1 0 3 3 1 6
3 1 1 0 3 3 6
4 1 1 0 1 3 6
4 3 6 0 1 3 6

2) The return output represent that the procedure is used for sorting the given list. As it swaps every element by its previous element if the previous number is greater than the next number.

3)

The worst case complexity of this algorithm is when the given list of number are sorted in reverse order that is in dexcending order.

In this case we will need to swap in each iteration of j that is.

when i=2 , (j =1)

when i=3 , (j = 2,j=1)

when i=4, (j =3, j=2, j=1)

For all these values we need to swap.

that is for every outer loop we will have multiple internal loop.

if we look at the above case we will have number of swap as:-

1+2+3+4-----(n-1)

so according to sumation we have n(n-1)/2 swaps that is multiple of n2 swaps

So the Big-O notation will be O(n2)

Hope it clears your doubt, in case of any other doubt do write in comments.

Please up vote if it helps you :)

Add a comment
Know the answer?
Add Answer to:
(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...
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
  • What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1...

    What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-

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

  • Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...

    Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...

  • Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...

    Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2 arbitrary integers. In the pseudo-code, "return” means immediately erit the procedure and then halt. Note that the indices of array A starts at 1. NOTHING(A) 1 n = A. size 2 for i = 1 ton // i=1,2,..., n (including n) 3 for j = 1 ton // j = 1,2,...,n (including n) 4. if A[n - j +1] + j then return 5...

  • Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do...

    Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 4. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a worst-case input of size n. Show your work. 5. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a best-case input of size n. Show your work.

  • 2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running...

    2. Suggest a structured plan (algorithm) for the bubble sort and selection sort, and perform running time analysis for both best and worst case. 3. Consider the age data of 12 children who are supposed to undergo for vaccination in ascending order of their age. Suggest and apply a sorting technique which can efficiently handle this data. Show all the intermediate steps clearly. Child ID 01 02 03 04 05 06 07 08 09 10 11 12 2. Age 1...

  • Find the worst case runtime f(n) for the following algorithms. Specify the number of operations executed...

    Find the worst case runtime f(n) for the following algorithms. Specify the number of operations executed for an input size n, for the worst case run time as a function of n. Circle statement(s) and draw a   line to the right side specifying the number of operations. If statement(s) are a part of an iteration of n, specify the total number of iterations as a function of n. Algorithm-01 int sum = 0; int j = 1; while ( <=...

  • Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an...

    Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an for i=1 to n-1 do for j=1 to n-i do if aj > aj+1 then interchange a; and a;+1 end if end for end for (b) Algorithm 1 describes a sorting algorithm called bubble sort for a list al,...,an of at least two numbers. Prove that the algorithm is complete, correct and terminates. (2) Complexity of Algorithms (Learning Target C2) (a) What is the...

  • Need to find number of elementary expressions in terms of n, not looking for Big O...

    Need to find number of elementary expressions in terms of n, not looking for Big O complexity. 4. Work out the number of elementary operations in the worst possible case and the best possible case for the following algorithm (justify your answer): 0: function Nonsense (positive integer n) 1: it1 2: k + 2 while i<n do for j+ 1 to n do if j%5 = 0 then menin else while k <n do constant number C of elementary operations...

  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

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