Θ(n2)
The variable i goes from 0 to n-1. The variable j goes from i+1 to n-1. So for i=0, j goes from 1 to n-1 or n-1 iterations, for i=1, j goes from 2 to n-1 or n-2 iterations, up to i=n-1 when j goes from n to n-1, which is 0 iterations since starting point is greater than ending point. So, the total number of iterations is (n-1)+(n-2)+....+1+0 = (n-1)(n-1+1)/2 = n(n-1)/2 = (n2-n)/2. In the best case of the function, the if-condition will always be false and none of the operations will be executed. So, only the comparison in the condition of the if will be executed inside the loop. So, it will have a total of (n2-n)/2 operations in the best case. So, the best case time complexity is Θ(n2) since the highest power of n is always selected.
Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n -...
#9 What is time complexity of fun()? int fun(int n) { int count = 0; for (int i = n; i > 0; i /= 2) for (int j = 0; j < i; j++) count += 1; return count; } Group of answer choices O(n^2) O(nLogn) O(n) O(nLognLogn)
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...
Determine the worst-case complexity of the algorithm in terms of n. // search a key in an array a [1..n] of length n for(int k = 1; k <= n; k++) if(a[k] == key) then return k; return -1; // meaning key not found
Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...
(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...
1. Consider the following well-known sorting algorithm, which is studied later in the book, with a counter inserted to count the number of key comparisons. ALGORITHM SortAnalysis(A[0..n − 1]) //Input: An array A[0..n − 1] of n orderable elements //Output: The total number of key comparisons made count ←0 for i ←1 to n − 1 do v ←A[i] j ←i − 1 while j ≥ 0 and A[j ]> v do count ←count + 1 A[j + 1]←A[j ]...
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;...
Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...
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.
Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...