Since here we have to calculate T(n) as worst case time complexity . To calculate worst case time complexity
let's assume that for all i and j , both 'if' statements given in program is False because otherwise function will be returned to main function and we might not get worst case for T(n) .
We will Calculate time complexity line by line
1. n = A.size ( This line will take constant time lets O(1) because it is just an arithmetic operation.)
time complexity for any for loop is number of time inner statement is executed.
2. for i=1 to n
3. for j=1 to n
4. if A[n-j+1] != j then return (this line is a inner statement for both for loops)
5. if (A[i]!=n-i) or (A[1]+A[2] = 2*n-3) then return ( this line is also a statement)
in case of nested for loops time complexity is number of times inner statement is executed.
here for every value of i , for loop of line 3 executes from j=1 to n means 'n' number of times.
Therefore number of times inner loop executes is = (n+n+n......('n' times)) = n(1+1+1+.....n times) = n 2
Therefore for line 2,3,4,5 total time complexity will be O(n 2)
so total time complexity T(n) = O(n 2) + C = O(n 2)
Now question is why it is O(n 2) and not Ω(n 2) that is because our upper bound is n2 and we will never execute
both nested for loops greater than n2 times .
Therefore Your Answer is O(n 2).
Question 1. (1 marks) The following procedure has an input array A[1..n] with n > 2...
Write a procedure that generates an array of Fibonacci numbers as its return value. The input to the procedure should be an integer n greater than or equal to two that specifies the desired size of the array. Here is how such a procedure would appear in a main(): int main() { long* fibs; fibs = make_fibs(10); for (int k=0; k<10; k++) cout << fibs[k] << " "; cout << endl; return 0; } This code should produce the output:...
In terms of n, what is the worst-case running time of countingSort on an input array of n letters from the alphabet (so k = 26, and n is arbitrary)? counting Sort(A[1 ...n]): // assume each A[i] € {1,2,...,k} 1: for v :=1 to k: 2: count[v] := 0 3: for i:=1 to n: 4: count[A[i]] := count[A[i]] +1 5: i:=1 6: for v:=1 tok: 7: for t :=1 to count[0]: 8: A[i]:= 0 9: i := i +1
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton - 1 do for j = i +1 to n - 1 do if A[i] + A[j] == K then count = count +1 end if end for end for return count What is the best case time complexity of the above algorithm?! (log(n)) O(1) (n) (na) Previous o H H 9
Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function. As in Assignment 18, print out the array before and after sorting. Use the...
Question One (2 marks): What is the complexity of the given code as a function of the problem size n? Show the (complete) details of your analysis. Note: a [ i] s an array with n elements. for (int i- 0; i < n; i++) if (Math.random) > 0.5) if (i%2-0) Insertionsort (a[i]); else Quicksort (a[i]) else for (int j = 0; j < i; j++) for (int k 0: k 〈 i; k++) o (logn) Question One (2 marks):...
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 an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...
Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...
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...