(a) It simply run 5*n2 times . Hence, its time complexity is O(n 2)
(b) There are two nested for loops. for each outer contains variable i, inner loop will run n-i times.
hence it runs n + n-1 + n-2....1 = n*(n+1)/2 times.
Hence , its time complexity is O(n 2)
(c) Here the value of j varies from
2,4,8....n
Hence it will run log2 n -1 times. Hence, its time complexity is O(log2 n) times
Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for...
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; } }
1. Give the running time of the following procedures, using -notation. Show your work. procedure P. (n) S=0; for i n to 2n do for j = 5i to 5i+12 do ses+i-j. procedure ps(n) 560; for i« 5n to 6n do for 15 to i do for k j to i do ses+i-j. procedure pe(n) S=0; for i1 to 5n do begin j4i ; while j<i' do begin 5 5 +i-j; je 5; end end
Specify the running time of each of the following algorithms. You must fully explain your answer for each of the question! Algorithm Ex1(ai,.., an), a): for i ← 1 to n do If ai 〉 a raj return x Algorithm Ex2((ai ,..., an), (b,..., bm)): for i ← 1 to min(n, m) do ifai 〈bi return x
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow 0 to n - 2 do for j leftarrow i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.
Q4) [5 points] Consider the following two algorithms: ALGORITHM 1 Bin Rec(n) //Input: A positive decimal integer n llOutput: The number of binary digits in "'s binary representation if n1 return 1 else return BinRec(ln/2)) +1 ALGORITHM 2 Binary(n) tive decimal integer nt io 's binary representation //Output: The number of binary digits in i's binary representation count ←1 while n >1 do count ← count + 1 return count a. Analyze the two algorithms and find the efficiency for...
Count the number of assignments and comparisons in this algorithms. a) Algorithm Loop1(n): p+ 1 for i = 1 to n do ppi b) Algorithm Loop2(n): SO for i = 1 to n do for j = 1 to i do Sesti
Find asymptotic running time , find expression for the running time as a function of n, then find valid upper and lower bound which differ by only a constant factor g) Func7(n) while (i 5n3) do 10n3 while 3) do s i j return 8 (h) Func8(n) for 1 to n do for i to n2 do for k j to n do s i -t- j return s (i) Func9(n) for i- 1 to n/2 do for j i...
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-
Write a recurrence relation describing the worst case running time of each of the following algorithms, and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution by using substitution or a recursion tree. You may NOT use the Master Theorem. Simplify your answers, expressing them in a form such as O(nk) or (nklog n) whenever possible. If the algorithm takes exponential time, then just give an exponential lower bound using the 2 notation. function...
Consider the following algorithm: a. What does this algorithm compute? b. Compute the running time of this algorithm. ALGORITHM Mystery(n) //Input: A nonnegative integer n for ← 1 to n do return S